Python AttributeError: 'numpy.float64' 对象没有属性 'log10'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47208473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:04:54  来源:igfitidea点击:

AttributeError: 'numpy.float64' object has no attribute 'log10'

pythonnumpy

提问by WolVes

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:

我正在尝试使用 sklearn.LinearRegression 找到大量短系列的对数斜率。数据是从 Pandas 数据帧的行中提取的,如下所示:

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:

但是,当我尝试使用 np.log10 时,在该系列中出现以下错误:

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.log10should work with numpy.float64 from what I am seeing. Ideas?

我不确定为什么会引发此错误,np.log10应该与 numpy.float64 一起使用。想法?

回答by Warren Weckesser

numpy.log10is 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 Seriesinstance, is passed to log10. The data type of testis object, which means that the elements in testcan be arbitrary Python objects. np.log10doesn't know how to handle such a collection of objects (it doesn't "know" that those objects are, in fact, all np.float64instances), 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 log10method. That's when the error occurs: the elements in the Series(in this case, np.float64instances) do not have a log10method.

numpy.log10是一个“ufunc”,并且该方法Series.apply(func)对 numpy ufuncs 有一个特殊的测试,它test.apply(log10)相当于np.log10(test). 这意味着test,一个 PandasSeries实例被传递给log10testis的数据类型object,这意味着其中的元素test可以是任意的 Python 对象。np.log10不知道如何处理这样的对象集合(它不“知道”这些对象实际上是所有np.float64实例),因此它尝试将计算分派到Series. 为此,它希望元素本身有一个log10方法。那是发生错误的时候Series:(在这种情况下,np.float64实例)中的元素没有log10方法。

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 Seriesobject from objectto np.float64.

应该做你想做的几个替代表达式是np.log10(test.astype(np.float64))or test.astype(np.float64).apply(np.log10)。关键部分是 test.astype(np.float64)Series对象的数据类型从 转换objectnp.float64

回答by Mylene Haslehner

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

使用标准偏差 (np.std) 而不是 np.log10 时,我收到了类似的错误消息:

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

'AttributeError: 'numpy.float64' 对象没有属性 'sqrt',

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

虽然我之前已经将 Pandas 对象 X 通过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)