2013-10-26 71 views
4

我試圖根據在與NumPy陣列用下面的代碼類型錯誤:「NoneType」對象不支持項目分配

X = np.arange(9).reshape(3,3) 
temp = X.copy().fill(5.446361E-01) 
ind = np.where(X < 4.0) 
temp[ind] = 0.5*X[ind]**2 - 1.0 
ind = np.where(X >= 4.0 and X < 9.0) 
temp[ind] = (5.699327E-1*(X[ind]-1)**4)/(X[ind]**4) 
print temp 

的特定索引值做一些數學計算,但我正在以下錯誤

Traceback (most recent call last): 
File "test.py", line 7, in <module> 
temp[ind] = 0.5*X[ind]**2 - 1.0 
TypeError: 'NoneType' object does not support item assignment 

請你幫我解決這個問題嗎? 謝謝

回答

2

fill什麼都沒有返回。

>>> import numpy as np 
>>> X = np.arange(9).reshape(3,3) 
>>> temp = X.copy() 
>>> return_value_of_fill = temp.fill(5.446361E-01) 
>>> return_value_of_fill is None 
True 

替換下列行:

temp = X.copy().fill(5.446361E-01) 

與:

temp = X.copy() 
temp.fill(5.446361E-01) 
+0

現在它顯示以下錯誤 - ValueError異常:陣列的具有多於一個元素的真值是不明確的。使用a.any()或a.all() –

+0

@RSJohn,將'ind = np.where(X> = 4.0和X <9.0)'替換爲'ind = np.where((X> = 4.0)& X <9.0))' – falsetru

+0

謝謝....現在它正在工作.... –

相關問題