【发布时间】:2017-10-28 22:18:10
【问题描述】:
我正在尝试计算 ndarray 的 log10,但我收到以下错误:AttributeError: 'float' object has no attribute 'log10',通过做一些研究我发现它与python 处理数值的方式,但我仍然不明白为什么会出现此错误。
>>> 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]])
我认为原因是 type(hx[0,0]) 是 Python 浮点类,但我也能够计算浮点类的 log10。我很确定我应该转换某种值,以便可以将其作为 numpy.log10() 的参数处理,但我无法发现它。
【问题讨论】:
-
你是如何创建
hx的?
标签: python numpy multidimensional-array