【问题标题】:TypeError: 'numpy.float64' object does not support item assignmentTypeError:“numpy.float64”对象不支持项目分配
【发布时间】:2020-03-02 19:31:40
【问题描述】:
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

错误:

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

出现在以下行:

        predictions[predictions > 1] = 1

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

【问题讨论】:

  • 错误发生在哪一行?您应该始终从 Python 发布您的“回溯”。
  • 您在分配predictions = np.transpose(predictions)[0] 时将predictions 设为标量。因此,当您尝试进一步向下执行 2 和 3 行时,您无法再对其进行索引。你想要完成什么?!
  • 对不起,这一行"predictions[predictions > 1] = 1": TypeError: 'numpy.float64' object does not support item assignment
  • @chen 你找到解决问题的方法了吗?可以给我看看吗?
  • FWIW,我在使用 numpy 0.17 和 csaps 0.11 时遇到了这样的错误。更新到 numpy 0.19 和 csaps 1.0 解决了这个问题。所以升级可能有助于解决这个does not support item assignment 错误。

标签: python numpy


【解决方案1】:

试试这个测试代码,关注np.array([1,2,3], dtype=np.float64)。 似乎 self.svm.decision_function(vectors) 返回 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

输出:

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

那么,你的向量是什么?

【讨论】:

  • 嗨,Max,如何知道向量是什么?我在同一代码上使用 Spider 上的调试,但我不知道如何找出向量变量中的内容
【解决方案2】:
    >>> 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

数组中没有元素大于1,所以你不能将1分配给predictions[predictions>1],你可以在分配前使用'predictions>1'。

【讨论】:

    【解决方案3】:
    predictions > 1
    

    是一个布尔运算。

    predictions[predictions > 1] = 1
    

    评估为

    predictions[True]
    

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

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

    【讨论】:

    • 这是 numpy 索引 (predictions[predictions > 1] = 1),这种语法可以在正确的上下文中工作
    【解决方案4】:

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

    预测 = 预测 / 2 + 0.5

    如果你想更新 'predictions' 包含的所有元素,不如执行以下操作

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

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

    预测[预测 > 1] = 1

    我不确定您要做什么,但是如果您要比较“预测”的每个索引处的元素是否大于 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
    

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 2023-02-26
      • 2016-07-31
      • 2014-02-18
      • 2017-03-26
      • 2021-08-05
      • 1970-01-01
      • 2022-08-08
      相关资源
      最近更新 更多