添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am attempting to find the log slope of a ton of short series using sklearn.LinearRegression. The data is being pulled from rows of a pandas dataframe and looks like:

bp01    1.12
bp02    1.12
bp03    1.08
bp04    0.99
bp05    1.08
bp06    1.19
bp07    1.17
bp08    1.05
bp09     0.8
bp10    0.96
bp11    0.97
bp12    1.12
bp13    0.91
bp14    0.96
bp15    1.05
bp16    0.93
bp17    0.97
bp18    0.92
bp19    0.89
bp20       0
Name: 42029, dtype: object

However, when I attempt to use np.log10, on the series I get the following error:

In[27]: test.apply(np.log10)
Traceback (most recent call last):
  File "<ipython-input-27-bccff3ed525b>", line 1, in <module>
    test.apply(np.log10)
  File "C:\location", line 2348, in apply
    return f(self)
AttributeError: 'numpy.float64' object has no attribute 'log10'

I am not sure why this error is being raised, np.log10 should work with numpy.float64 from what I am seeing. Ideas?

test is a pd.series though. The apply should be passing each value in the series the np.log10 command though right? – WolVes Nov 9, 2017 at 17:55

numpy.log10 is a "ufunc", and the method Series.apply(func) has a special test for numpy ufuncs which makes test.apply(log10) equivalent to np.log10(test). This means test, a Pandas Series instance, is passed to log10. The data type of test is object, which means that the elements in test can be arbitrary Python objects. np.log10 doesn't know how to handle such a collection of objects (it doesn't "know" that those objects are, in fact, all np.float64 instances), so it attempts to dispatch the calculation to the individual elements in the Series. To do that, it expects the elements themselves to have a log10 method. That's when the error occurs: the elements in the Series (in this case, np.float64 instances) do not have a log10 method.

A couple alternative expression that should do what you want are np.log10(test.astype(np.float64)) or test.astype(np.float64).apply(np.log10). The essential part is that test.astype(np.float64) converts the data type of the Series object from object to np.float64.

I had a similar error message when using the standard deviation (np.std) instead of np.log10:

'AttributeError: 'numpy.float64' object has no attribute 'sqrt',

and this although I had previously converted the Pandas object X to a numpy array via np.asarray(X).

I could solve this problem by applying the above-mentioned solution:

X = pd.read_excel('file.xls')
Y = np.asarray(X).astype(np.float64)
Z = np.std(Y,axis=0)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.