deviations = returns - returns.mean() squared_deviations =
deviations**2 variance = squared_deviations.mean()
import numpy as np
volatility = np.sqrt(variance) volatility
When I run this code I get this error
'function' object has no attribute 'mean'
I am confused as I have written this text before and it seems to have worked fine. I even do simple stuff like returns.std() and I get the same response.
'function' object has no attribute even after importing pandas
I have already imported NumPy and pandas
Related
works:
import sympy as sp
import pandas as pd
test = pd.DataFrame( {'greeting':['hey','hi','hello','howdy']} )
test1['greeting'].map(lambda x: sp.Set(*x).is_proper_subset( sp.Set(*x) ))
doesn't work:
import sympy as sp
import pandas as pd
test = pd.DataFrame( {'greeting':['hey','hi','hello','howdy']} )
test1['greeting'].map(lambda x: sp.Set('hi',).is_proper_subset( sp.Set(*x) ))```
i get:
AttributeError: 'str' object has no attribute 'args'
i've also tried with numbers on another dataframe:
test['triplets'].map(lambda x: sp.Set(*(813,)).is_proper_subset( sp.Set(*x) ))
and i get the same result.
https://docs.sympy.org/latest/modules/sets.html
class sympy.sets.sets.Set(*args)[source]¶
The base class for any kind of set.
ok...why is it working for each Series value when i pass it through lambda, but not when i write it manually
note: my end goal is to have this inside a for loop where the the iteration is passed into where i have 'hi'
From sympy's Set documentation:
class sympy.sets.sets.Set(*args)[source]¶
This is not meant to be used directly as a container of items. It does not behave like the builtin set; see FiniteSet for that.
The main problem is that is_proper_subset is meant for Interval type of "sets". It doesn't seem to handle simpler sets well, but gives a rather cryptic error message. When debugging, it often helps to reduce the problem as much as possible:
import sympy as sp
sp.Set(1, 2).is_proper_subset(sp.Set(1, 2, 3))
This raises the error AttributeError: 'int' object has no attribute 'args'.
Similarly:
sp.Set('a', 'b').is_proper_subset(sp.Set('a', 'b', 'c'))
Leads to AttributeError: 'str' object has no attribute 'args'.
The best solution to the original problem, is to use standard python functions.
i don't know what the deal is, i'm hoping there's an answer - but until then, i've resorted to this:
test1['greeting'].map(lambda x: set('hi').issubset(set(x)) and set('hi')!=set(x))
I am dealing with error
"AttributeError: 'numpy.ndarray' object has no attribute 'nipy_spectral'"
while running silhouette analysis.
The original code was taken from here. I have added the line that shows the error:
color = cm.nipy_spectral(float(i) / n_clusters)
The problem was that you were assigning cm to some other array. Correct way is not to assign cm to any other array or change the code to
matplotlib.cm.nipy_spectral(float(i) / n_clusters)
Change it at all the places where cm is used.
Here is the snippet of the input data and the result of running print(type(cm)).
Screenshot
I am getting the error below after running the code at end. Please let me know how to solve it. I am importing pandas, numpy before.
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'nunique'
train_dt = pd.DataFrame(train.dtypes,columns = ['Numpy Dtype'])
train_dt['Nunique'] = train.nunique()
You need to upgrade pandas because DataFrame.nunique is implemented in pandas 0.20.0:
DataFrame.nunique(axis=0, dropna=True)
Return Series with number of distinct observations over requested axis.
New in version 0.20.0.
make sure you write your line code true
Example:
True way:
`datafram.groupby('column')['column'].nunique()` #this is true way
wrong way:
`datafram.groupby('column'),['column'].nunique()` #this way give you your error
So I have been writing a code to standardize the elements of a matrix and the function I used is as follows:
def preprocess(Data):
if stdn ==True:
st=np.empty((Data.shape[0],Data.shape[1]))
for i in xrange(0,Data.shape[0]):
st[i,0]=Data[i,0]
for i in xrange(1,Data.shape[1]):
st[:,i]=((Data[:,i]-np.min(Data[:,i]))/(np.ptp(Data[:,i])))
np.random.shuffle(st)
return st
else:
return Data
It works very well outside the class but when used inside of it it gives me this error:
AttributeError: 'tuple' object has no attribute 'shape'
Any idea on how I can fix it??
P.S. This is a KNN classification code
According to the error you posted, Data is of type tuple and there is no attribute shape defined for data. You could try casting Data when you call your preprocess function, e.g.:
preprocess(numpy.array(Data))
I'm trying to reproduce the example given here: http://jkitchin.github.io/blog/2013/02/12/Nonlinear-curve-fitting-with-parameter-confidence-intervals/
So I imported the module like that:
from scipy.stats.distributions import t
But when I try to a simple
tval = t.ppf(1-alpha/2, dof)
I have the exception:
AttributeError: 'numpy.ndarray' object has no attribute 'ppf'
So t is a numpy.ndarray. But if I read the doc, it is supposed to be an object, with methods.
Do you have an idea about what's happening ?
It seems you may have overwritten the variable t with an array somewhere. What your error message means is that t is a numpy.ndarray which has no ppf method. The t you intended to import shouldn't be an ndarray but rather a distribution generator.
Either find where it became an array and use another name there, or import with better names.
For example, try changing your import line to this:
from scipy.stats import distrbutions as dists
and then change the problem line to:
tval = dists.t.ppf(1-alpha/2, dof)
Alternatively:
from scipy.stats.distributions import t as tdist
tval = tdist.ppf(1-alpha/2, dof)