TypeError:'numpy.float64'对象不支持项目分配 [英] TypeError: 'numpy.float64' object does not support item assignment
本文介绍了TypeError:'numpy.float64'对象不支持项目分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
有人有解决这个问题的想法吗?谢谢!
Does anyone has an idea of solving this problem? Thanks!
推荐答案
尝试此测试代码,并注意np.array([1,2,3], dtype=np.float64)
.
似乎self.svm.decision_function(vectors)返回 1d 数组,而不是2d.
如果将[1,2,3]替换为[[1,2,3],[4,5,6]],一切都会好的.
Try this testing code and pay attention to np.array([1,2,3], dtype=np.float64)
.
It seems self.svm.decision_function(vectors) returns 1d array instead of 2d.
If you replace [1,2,3] to [[1,2,3], [4,5,6]] everything will be ok.
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
那么,您的载体是什么?
So, what your vectors are?
这篇关于TypeError:'numpy.float64'对象不支持项目分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文