2017-10-28 147 views
0

我試圖計算一個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()的參數處理,但我無法發現它。

+0

你是如何創建'hx'? –

回答

2

hx的數據類型爲object。您可以在輸出中看到,並且您可以檢查hx.dtype。存儲在數組中的對象顯然是Python浮點數。 Numpy不知道你可能在對象數組中存儲了什麼,所以它試圖將其功能(例如log10)分派給數組中的對象。這會失敗,因爲Python浮動方法沒有log10方法。

在你的代碼的開頭試試這個:

hx = hx.astype(np.float64) 
+0

如果錯誤是由numpy內的調用觸發的,爲什麼不是在回溯中給出的代碼? –

相關問題