【问题标题】:'numpy.float64' object does not support item assignment“numpy.float64”对象不支持项目分配
【发布时间】:2019-01-03 02:45:34
【问题描述】:

我有“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

这是我的代码:

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])

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

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

【问题讨论】:

  • 你会为minimal, complete, and verifiable example添加一些简单的数据吗?
  • vec = np.array((1000,2422), dtype=np.uint8), sim = np.array((1000,1), dtype=np.float64) 这些元组应该是数组的形状吗?如果是这样,那么您使用它们是错误的。在 np.onesnp.zeros 等填充数组中使用 shape 参数。
  • @jezrael 嗨,jezrael!是的,但我是新手,我在上传链接或照片时遇到了一点问题,但我使用了这个链接的数据:(kaggle.com/PromptCloudHQ/imdb-data)
  • @user2285236 是的,我也尝试过使用这些表单,但我得到了同样的错误。
  • @user2285236 问题是我想念它们的复杂变量,但是使用 np.array 它没有解决,我使用了 np.ones正如你所说,你知道为什么会这样吗?

标签: python pandas numpy dataframe typeerror


【解决方案1】:

我发现这个问题是如何发生的,我已经为我的数组分配了浮点变量,但是当我使用 fillnamean 函数,然后在内部积中返回一些 complex 变量,所以我将数组类型更改为这种形式:

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

问题就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-26
    • 2021-02-14
    • 2019-11-14
    • 2015-03-09
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多