4

次のコードを使用して、NumPy 配列の特定のインデックスの値に従って数学的な計算を実行しようとしています。

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

これを解決するのを手伝ってくれませんか?ありがとう

4

1 に答える 1

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)
于 2013-10-26T09:50:15.670 に答える