I am new to numpy and I am NOT understanding the documentation as regards diff. the code below throws the error. I am baffled any help would be appreciated.
Traceback (most recent call last):
File "/home/dave/Desktop/mcmtest/testhv calc.py", line 11, in <module>
r = np.log(close_prices).diff()
AttributeError: 'numpy.ndarray' object has no attribute 'diff'
here is the test code.
import numpy as np
from numpy import sqrt,mean,log,diff
import pandas as pd
close_prices = [178.97,175.5,171.07,171.85,172.43,172.99,167.37,164.34,162.71,\
156.41,155.15,159.54,163.03,156.49,160.5,167.78,167.43,166.97,167.96,171.51,171.11]
print (close_prices)
r = np.log(close_prices).diff()
print(r)
Given that numpy.ndarray is the Python type of "numpy arrays", the error is just saying that arrays don't have a diff method. diff is a function defined in the numpy module.
Instead of np.log(close_prices).diff(), do
np.diff(np.log(close_prices))
Related
I'm still learning to use FatSym to analyze medical images; But I keep getting this error 'FlatSym' object has no attribute 'array' This is my code
pip install numpy
pip install pylinac
from pylinac import FlatSym
my_file = r"C:\Users\xxxx.dcm"
my_img = FlatSym(path=my_file)
my_img.analyze(flatness_method='varian', symmetry_method='varian', vert_position=0.5, horiz_position=0.5)
AttributeError Traceback (most recent call last)
<ipython-input-25-0bda45c5e24c> in <module>
----> 1 my_img.analyze(flatness_method='varian', symmetry_method='varian', vert_position=0.5, horiz_position=0.5)
C:\ProgramData\Anaconda3\lib\site-packages\pylinac\flatsym.py in analyze(self, flatness_method, symmetry_method, vert_position, horiz_position, vert_width, horiz_width)
C:\ProgramData\Anaconda3\lib\site-packages\pylinac\flatsym.py in _calc_symmetry(self, method, vert_position, horiz_position, vert_width, horiz_width)
C:\ProgramData\Anaconda3\lib\site-packages\pylinac\flatsym.py in _get_vert_profile(self, vert_position, vert_width)
AttributeError: 'FlatSym' object has no attribute 'array'
What i'm missing here please? Thanks!
FlatSym you have imported is an object try this
from pylinac import FlatSym
my_file = r"C:\Users\xxxx.dcm"
my_img = FlatSym(my_file)
I would like to use the pandas package for python. Some functionalities work, but when I try to pass "include" argument into the describe() function I get an error:
train_df.describe(include=['O'])
Full code looks like thie following:
import numpy as np
import pandas as pd
import random as rnd
import matplotlib.pyplot as plt
# aquire data
train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('test.csv')
train_df.describe(include=['O'])
I get the following error:
>> python survival.py
Traceback (most recent call last):
File "survival.py", line 10, in <module>
train_df.describe(include=['O'])
TypeError: describe() got an unexpected keyword argument 'include'
Using the .describe() on its own seems to work. Any ideas? Thank you.
ok so I'm trying to write this simple call request for YouTube channel data but it seems I'm still too much of a noob to fully understand what I'm doing wrong I understand that there is some type of syntax error but what I want to fully understand is why is there a syntax error so I can easily solve this issue in the future. I've spent too many hours trying to figure out the error here I know an experienced coder could solve this issue in a matter of minute so can someone help please.
Here is the full list of error terminal spits back
line 8, in channel_list_scrape
list_channel_attr = youtube.channels.list(id=youtube_channel).execute()
AttributeError: 'function' object has no attribute 'list'
line 11, in <module>
channel_list_scrape(youtube_channel = 'CNN')
Code:
from apiclient.discovery import build
import csv
def channel_list_scrape(youtube_channel):
DEVELOPER_KEY = 'string_would_go_in_here'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
list_channel_attr = youtube.channels.list(id=youtube_channel).execute()
return(list_channel_attr)
channel_list_scrape(youtube_channel = 'CNN')
I do not know the specifics of the api you are using, but from the traceback it sounds like you need to do something like this:
list_channel_attr = youtube.channels().list(id=youtube_channel).execute()
From what it it looks like, you want to call the list() method of whatever object is returned by youtube.channels(). What you are doing now is calling list() on the youtube.channels method object itself, rather than calling it on the object that method returns.
To further illustrate, observe the following interactive session with explanations in the comments:
In [1]: def foo(): # This function returns a list
...: return [1, 2, 3]
...:
...:
In [2]: [1, 2, 3].pop() # lists have a pop method
Out[2]: 3
In [3]: foo().pop() # so the return value of the function also has a pop method
Out[3]: 3
In [4]: foo.pop() # but the function itself does not have a pop method
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-20ec23cbc1ac> in <module>()
----> 1 foo.pop()
AttributeError: 'function' object has no attribute 'pop'
I am trying to create a countplot with sns. I adapted the following code:
sns.countplot(x="deck", data=titanic, palette="Greens_d")
I use a data frame called dfvp where XP is a categorical variable which can take two string values (either defense or prosecution).
here is my adapted code:
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
Here is the error message that I get:
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
Traceback (most recent call last):
File "<ipython-input-220-20d65ae5d282>", line 1, in <module>
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
AttributeError: 'module' object has no attribute 'countplot'
FYI: I use ANACONDA and Python 3.4. on a PC with Windows 8.
Could you tell me how to fix this/what I am doing wrong?
EDIT:
Here is a MCVE
import seaborn as sns
dfvp = read_csv('C:\\Users\\VP_Prod_study_2_data changed_3.csv')
sns.countplot(x="XP", data=dfvp, palette="Greens_d")
I'm fresh on python,using python2.7,and got some questions on the code blew:
import numpy as np
from scipy import interpolate
import pylab as py
x=np.r_[0:10:11j]
y=np.sin(x)
xnew=np.r_[0:10:100j]
#f=interpolate.interpld(x,y)
py.figure(1)
py.clf()
py.plot(x,y,'ro')
for kind in ['nearest','zero','slinear','quadtatic','cublic']:
f=interpolate.interpld(x,y,kind=kind)
ynew=f(xnew)
py.plot(xnew,ynew,label=kind)
py.legend(loc='lower right')
but it resulted in:
Traceback (most recent call last):
File "C:\Users\LCL\.xy\startups\python_web\my_first_try_on_python_web\python_Interpolation\example1.py", line 22, in <module>
f=interpolate.interpld(x,y,kind=kind)
AttributeError: 'module' object has no attribute 'interpld'
You used interpld, i.e. INTERPLD.
You want interp1d, i.e. with the numeral 1, for one-dimensional.
It looks like you are using interpolate.interpld but the function name is interpolate.interp1d (with a number one instead of a letter L).
It should be "interp1d",the number 1,not L