Python 类型错误:'numpy.float64' 对象不支持项目分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27915306/
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-08-19 02:27:52  来源:igfitidea点击:

TypeError: 'numpy.float64' object does not support item assignment

pythonnumpy

提问by chen

def classify(self, texts):
        vectors = self.dictionary.feature_vectors(texts)
        predictions = self.svm.decision_function(vectors)
        predictions = np.transpose(predictions)[0]
        predictions = predictions / 2 + 0.5
        predictions[predictions > 1] = 1
        predictions[predictions < 0] = 0
        return predictions

The error:

错误:

TypeError: 'numpy.float64' object does not support item assignment

occurs on the following line:

发生在以下行:

        predictions[predictions > 1] = 1

Does anyone has an idea of solving this problem? Thanks!

有没有人有解决这个问题的想法?谢谢!

回答by Max Tkachenko

Try this testing code and pay attention to np.array([1,2,3], dtype=np.float64). It seems self.svm.decision_function(vectors) returns 1darray instead of 2d. If you replace [1,2,3] to [[1,2,3], [4,5,6]] everything will be ok.

试试这个测试代码并注意np.array([1,2,3], dtype=np.float64). 这似乎self.svm.decision_function(载体)返回1D阵列而不是2D。如果将 [1,2,3] 替换为 [[1,2,3], [4,5,6]] 一切都会好的。

import numpy as np
predictions = np.array([1,2,3], dtype=np.float64)
predictions = np.transpose(predictions)[0]
predictions = predictions / 2 + 0.5
predictions[predictions > 1] = 1
predictions[predictions < 0] = 0

Output:

输出:

Traceback (most recent call last):
  File "D:\temp\test.py", line 7, in <module>
    predictions[predictions > 1] = 1
TypeError: 'numpy.float64' object does not support item assignment

So, what your vectors are?

那么,你的向量是什么?

回答by john ktejik

predictions > 1

is a boolean operation.

是一个布尔运算。

predictions[predictions > 1] = 1

evaluates to

评估为

predictions[True]

You are looking for the np.where()operator. Your code should look like this:

您正在寻找np.where()运算符。您的代码应如下所示:

predictions[np.where(predictions > 1)] = 1

回答by Danny Wu

    >>> predictions = np.array([1,2,3], dtype=np.float64)
    >>> predictions
    array([1., 2., 3.])
    >>> predictions = np.transpose(predictions)[0]
    >>> predictions
    1.0
    >>> predictions = predictions / 2 + 0.5
    >>> predictions
    1.0
    >>> predictions>1
    False

there is no element in array is bigger than 1,So you cannot assign 1 to predictions[predictions>1],you can use ' predictions>1 ' before your assignment.

数组中没有元素大于 1,因此您不能将 1 分配给预测 [predictions>1],您可以在分配之前使用 'predictions>1'。

回答by Sibulele

You can't perform this operation in all elements at once,

您不能一次在所有元素中执行此操作,

predictions = predictions / 2 + 0.5

预测 = 预测 / 2 + 0.5

If you want to update all the elements contained by 'predictions', rather do the following

如果要更新“预测”包含的所有元素,请执行以下操作

for i in range(predictions):
   predictions[i] = predictions[i] / 2 + 0.5

What are you doing here ???????????

你在这里做什么 ???????????

predictions[predictions > 1] = 1

预测[预测 > 1] = 1

I am not sure of what you are trying to do , but if you are trying to compare if elements at each index of 'predictions' is greater than 1, rather put the statements under the above if statement as follows

我不确定您要做什么,但是如果您要比较“预测”的每个索引处的元素是否大于 1,请将语句放在上面的 if 语句下,如下所示

for i in range(predictions):
  predictions[i] = predictions[i] / 2 + 0.5
  if predictions[i] > 1:
     predictions[i] = 1
  elif predictions[i] < 0 : 
     predictions[i] = 0

return predictions