21

エラーは次のようになります。

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-trainを使用しています。はy_trainLIBSVM 形式です。これはX機能:値のペアですy_train。誰かがこれを修正するのを手伝ってくれますか? どうもありがとう!X_trainshrink_threshold.todense()X_train

4

3 に答える 3

41

Pystruct を使用して同様の問題が発生しましたpystruct.learners.OneSlackSSVM

これは、トレーニング ラベルが整数ではなく浮動小数点数であったために発生しました。私の場合、dtype=np.int8 を指定せずに、ラベルを np.ones で初期化したことが原因でした。それが役に立てば幸い。

于 2014-06-17T10:42:57.483 に答える
7

インデックス配列integerが作成された方法で明確に型付けされている必要があることはよくありますが、渡された空のリストの場合float、プログラマーが考慮しない可能性のあるケースが default になります。例えば:

>>> np.array(xrange(1))
>>> array([0])                #integer type as expected
>>> np.array(xrange(0))
>>> array([], dtype=float64)  #does not generalize to the empty list

dtypeしたがって、配列コンストラクターで常に明示的に定義する必要があります。

于 2018-04-24T12:32:20.627 に答える