I am trying to execute the following code:
sapmle2000submission.astype('int32').dtypes
which raises an error:
AttributeError Traceback (most recent call
last) in ()
----> 1 sapmle2000submission.astype('int32').dtypes
AttributeError: 'list' object has no attribute 'astype'
Can someone please help me to figure out why?
Looks like that the obejct sapmle2000submission is a list, not a pandas Series.
You can convert it as Series and specify its dtype:
import pandas as pd
sapmle2000submission_series = pd.Series(sapmle2000submission, dtype='int32')
df3.rename(columns={'Impressions': 'Impressions Served'}, inplace=True)
df3.rename(columns={'Impressions': 'Impressions Served'}, inplace=True)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-6cd7fcf0d1d0> in <module>()
----> 1 df3.rename(columns={'Impressions': 'Impressions Served'}, inplace=True)
AttributeError: 'str' object has no attribute 'rename'
Can you tell me why I'm having this error and how to change the name of the column in my df?
df3 is not of type pandas.DataFrame, it is a string. Therefore your issue is with whatever code creates the df3 variable. If you share a minimal reproducible version of the code that creates df3 then you might get help fixing that issue.
I have 2 issues nested as one:
n_rows, n_cols = np.shape(Z)
ZT = Z.transpose()
ZTZ = np.dot(ZT,Z) # does return a value
ZTZ1 = np.matmul(ZT,Z) # error
print("Close?")
print(np.allclose(ZTZ,ZTZ1))
print("----")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-211-f26bdaebc910> in <module>()
26
27 print
---> 28 coV = getCovariance(df)
29 #print(coV)
30 print
<ipython-input-211-f26bdaebc910> in getCovariance(df)
13 ZT = Z.transpose()
14 ZTZ = np.dot(ZT,Z)
---> 15 ZTZ1 = np.matmul(ZT,Z)
16 print("Close?")
17 print(np.allclose(ZTZ,ZTZ1))
AttributeError: 'module' object has no attribute 'matmul'
Okay ... so obviously matmul doesn't exist on my machine. Got it. Now how do I confirm that the dot is doing the same thing? Because I have a matrix that was once a pandas.DataFrame object and I converted it to a matrix through it's .as_matrix() method and I am getting rounding errors and need to check where things went wrong ... I also tried the standard * operator, but that doesn't work either on np.ndarray matrix objects.
SIDE NOTE: if there are any pro tips on rounding that could be transferred from someone with experience with pandas, that is also much appreciated because I can't seem to find out how pandas has given me a different matrix than a build in function from the numpy class (I have been asked to reimplement the function).
I am trying to run Kmeans clustering on a data. My data frame is a pandas data frame which is of following dimensions.
People_reduced.shape
Out[155]:
(417837, 13)
Now while k-means is running fine, when I try to feed the output of Kmeans cluster labels and the original data frame to silhouette_score method of sklearn , it is throwing a weird error.
Here is the code I used:
kmeans=KMeans(n_clusters=2,init='k-means++',n_init=10, max_iter=20)
kmeans.fit(People_reduced.ix[:,1:])
cluster_labels = kmeans.labels_
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(People_reduced.ix[:,1:].values,cluster_labels)
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-154-b392e118f64a> in <module>()
19 # This gives a perspective into the density and separation of the formed
20 # clusters
---> 21 silhouette_avg = silhouette_score(People_reduced.ix[:,1:].values,cluster_labels)
22 #silhouette_avg = silhouette_score(People_reduced.ix[:,1:], cluster_labels)
23
TypeError: 'list' object is not callable
Hi I am getting the following error
'numpy.ndarray' object is not callable
when performing the calculation in the following manner
rolling_means = pd.rolling_mean(prices,20,min_periods=20)`
rolling_std = pd.rolling_std(prices, 20)`
#print rolling_means.head(20)
upper_band = rolling_means + (rolling_std)* 2
lower_band = rolling_means - (rolling_std)* 2
I am not sure how to resolve, can someone point me in right direction....
The error TypeError: 'numpy.ndarray' object is not callable means that you tried to call a numpy array as a function. We can reproduce the error like so in the repl:
In [16]: import numpy as np
In [17]: np.array([1,2,3])()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/user/<ipython-input-17-1abf8f3c8162> in <module>()
----> 1 np.array([1,2,3])()
TypeError: 'numpy.ndarray' object is not callable
If we are to assume that the error is indeed coming from the snippet of code that you posted (something that you should check,) then you must have reassigned either pd.rolling_mean or pd.rolling_std to a numpy array earlier in your code.
What I mean is something like this:
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Works
Out[3]: array([ nan, nan, nan])
In [4]: pd.rolling_mean = np.array([1,2,3])
In [5]: pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Doesn't work anymore...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/user/<ipython-input-5-f528129299b9> in <module>()
----> 1 pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Doesn't work anymore...
TypeError: 'numpy.ndarray' object is not callable
So, basically you need to search the rest of your codebase for pd.rolling_mean = ... and/or pd.rolling_std = ... to see where you may have overwritten them.
Also, if you'd like, you can put in reload(pd) just before your snippet, which should make it run by restoring the value of pd to what you originally imported it as, but I still highly recommend that you try to find where you may have reassigned the given functions.
For everyone with this problem in 2021, sometimes you can have this problem when you create
a numpy variable with the same name as one of your function, what happens is that instead of calling the function python tries to call the numpy array as a function and you get the error, just change the name of the numpy variable
I met the same question and the solved.
The point is that my function parameters and variables have the same name.
Try to give them different name.