为什么这个数组没有属性'log10'?
问题描述:
我试图计算一个ndarray的log10,但我得到以下错误:AttributeError:'float'对象没有属性'log10',通过做一些研究,我发现它与方法有关python处理数值,但我仍然无法得到为什么我得到这个错误。为什么这个数组没有属性'log10'?
>>> hx[0:5,:]
array([[0.0],
[0.0],
[0.0],
[0.0],
[0.0]], dtype=object)
>>> type(hx)
<class 'numpy.ndarray'>
>>> type(hx[0,0])
<class 'float'>
>>> test
array([[ 0.],
[ 0.],
[ 0.]])
>>> type(test)
<class 'numpy.ndarray'>
>>> type(test[0,0])
<class 'numpy.float64'>
>>> np.log10(test)
array([[-inf],
[-inf],
[-inf]])
>>> np.log10(hx[0:5,:])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'log10'
>>> np.log10(np.float64(0))
-inf
>>> np.log10([np.float64(0)])
array([-inf])
>>> np.log10([[np.float64(0)]])
array([[-inf]])
>>> np.log10(float(0))
-inf
>>> np.log10([[float(0)]])
array([[-inf]])
我认为原因是,式(HX [0,0])是一个Python浮动类,但我能够计算浮子类的日志10为好。我非常确定我应该投入某种价值,以便它可以作为numpy.log10()的参数处理,但我无法发现它。
答
hx
的数据类型为object
。您可以在输出中看到,并且您可以检查hx.dtype
。存储在数组中的对象显然是Python浮点数。 Numpy不知道你可能在对象数组中存储了什么,所以它试图将其功能(例如log10
)分派给数组中的对象。这会失败,因为Python浮动方法没有log10
方法。
在你的代码的开头试试这个:
hx = hx.astype(np.float64)
+0
如果错误是由numpy内的调用触发的,为什么不是在回溯中给出的代码? –
你是如何创建'hx'? –