pandas 'numpy.float64' 对象不支持项目分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51533162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:50:46  来源:igfitidea点击:

'numpy.float64' object does not support item assignment

pythonpandasnumpydataframetypeerror

提问by yasi

I have Dataframe of "IMDB data from 2006 to 2016" which is in Kaggle site: https://www.kaggle.com/PromptCloudHQ/imdb-data. I have made it as numpy array but when I want to assign the inner product of two row of it to numpy.float64 variable, it gives me this error:

我有“2006 年到 2016 年的 IMDB 数据”的数据框,它位于 Kaggle 站点:https://www.kaggle.com/PromptCloudHQ/imdb-data 。我已经将它作为 numpy 数组,但是当我想将它的两行的内积分配给 numpy.float64 变量时,它给了我这个错误:

sim[i][1] = np.inner(vec[i],vec[1])
TypeError: 'numpy.float64' object does not support item assignment

here is my code:

这是我的代码:

X = trainset.drop(['Description', 'Runtime','Director','Title', 'ID'], axis=1)
X.Revenue = X.Revenue.fillna(X.Revenue.mean())
X.Metascore= X.Metascore.fillna(X.Revenue.min())
features = ['Genre','Actors']
for f in features:
    X_dummy = X[f].str.get_dummies(',').add_prefix(f + '.')
    X = X.drop([f], axis = 1)
    X = pd.concat((X, X_dummy), axis = 1)
vec = np.ones((1000,2422), dtype=np.uint8)
vec = X.values
sim = np.ones((1000,1), dtype=np.float64)
for i in range (1,1000):
    sim[i][1] = np.inner(vec[i],vec[1])

and when I get the type of this inner product it gives me exactly this type:

当我得到这个内积的类型时,它给了我这种类型:

>>chi = np.inner(vec[0],vec[0])
>>print(type(chi))
<class 'numpy.float64'>

回答by yasi

I find how this problem happened, I have assigned float variable to my array but when I used fillnawith meanfunction and after that in inner product it returns some complexvariable so I changed the array type to this form:

我发现了这个问题是如何发生的,我已经将 float 变量分配给了我的数组,但是当我将fillnamean函数一起使用时,然后在内积中它返回一些复杂的变量,因此我将数组类型更改为这种形式:

sim = np.ones((1000,1), dtype=np.complex_)

and the problem was solved.

问题就解决了。