AttributeError:"numpy.float64"对象没有属性"log10" [英] AttributeError: 'numpy.float64' object has no attribute 'log10'

查看:195
本文介绍了AttributeError:"numpy.float64"对象没有属性"log10"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sklearn.LinearRegression查找一吨短序列的对数斜率.数据是从熊猫数据帧的行中提取的,如下所示:

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

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

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'

我不确定为什么会出现此错误, np.log10 应该可以与numpy.float64一起使用.想法?

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

推荐答案

numpy.log10是一个"ufunc",方法Series.apply(func)对numpy ufuncs进行了特殊测试,使test.apply(log10)等效于np.log10(test).这意味着test(熊猫Series实例)将传递到log10. test的数据类型为object,这意味着test中的元素可以是任意Python对象. np.log10不知道如何处理这样的对象集合(它不知道"这些对象实际上是所有np.float64实例),因此它尝试将计算分派到其中的各个元素. Series.为此,它期望元素本身具有log10方法.这就是发生错误的时间:Series中的元素(在本例中为np.float64实例)没有log10方法.

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.

应该执行您想要的操作的两个替代表达式是np.log10(test.astype(np.float64))test.astype(np.float64).apply(np.log10).重要的部分是test.astype(np.float64)Series对象的数据类型从object转换为np.float64.

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.

这篇关于AttributeError:"numpy.float64"对象没有属性"log10"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆