2013-06-30 41 views
7

錯誤是這樣的:陣列必須是整數(或布爾)類型的

Traceback (most recent call last): 
    File "NearestCentroid.py", line 53, in <module> 
    clf.fit(X_train.todense(),y_train) 
    File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.13.1-py2.7-linux-i686.egg/sklearn/neighbors/nearest_centroid.py", line 115, in fit 
    variance = np.array(np.power(X - self.centroids_[y], 2)) 
IndexError: arrays used as indices must be of integer (or boolean) type 

碼是這樣的:

distancemetric=['euclidean','l2'] 
for mtrc in distancemetric: 
for shrkthrshld in [None]: 
#shrkthrshld=0 
#while (shrkthrshld <=1.0): 
    clf = NearestCentroid(metric=mtrc,shrink_threshold=shrkthrshld) 
    clf.fit(X_train.todense(),y_train) 
    y_predicted = clf.predict(X_test.todense()) 

我使用scikit-learn包,X-trainy_train是在LIBSVM格式中,X是特徵:值對,y_train是目標/標籤,X_train是CSR基本格式,shrink_threshold不支持CSR稀疏矩陣,因此我將.todense()X_train,然後我得到了這個錯誤,有誰能幫我解決這個問題嗎?非常感謝!

+0

'y_train'的價值是什麼? – mbatchkarov

+0

我也見證了相同功能的參數略有不同的錯誤。 – bearrito

+0

您應該接受答案 – vumaasha

回答

19

我有一個類似的問題,使用Pystruct pystruct.learners.OneSlackSSVM

它發生是因爲我的訓練標籤是漂浮物,而不是整數。就我而言,這是因爲我用np.ones初始化了標籤,而沒有指定dtype = np.int8。希望能幫助到你。

+2

Man ..沒有任何有用的錯誤消息。我在過去3小時掙扎着。你救了我。 – vumaasha

+0

也救了我。謝謝。 –

相關問題