2015-01-13 142 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 
相關問題