2015-01-13 144 views
0
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 

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

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

发生在下面一行:

 predictions[predictions > 1] = 1 

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

+3

在哪一行发生错误?你应该总是从Python发布你的“追溯”。 –

+3

当您将'predictions = np.transpose(predictions)[0]'分配给''时,您已将'predictions'设为标量。因此,当您尝试进一步向下执行2到3行时,您无法再索引它。你想达到什么目的?! –

+0

抱歉,此行“预测[预测> 1] = 1”: TypeError:'numpy.float64'对象不支持项目分配 – chen

回答

1

试试这个测试代码,并注意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 

输出:

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 

那么,你的载体?

+0

嗨,马克斯,如何知道向量是什么?我在Spider上使用相同的代码进行调试,但我不知道如何找出向量变量中的内容 – user1314404

0
predictions > 1 

是一个布尔操作。

predictions[predictions > 1] = 1 

评估为

predictions[True] 

您正在寻找np.where()操作。你的代码应该是这样的:

predictions[np.where(predictions > 1)] = 1 
相关问题