2016-10-06 5 views
1

私はSVMを使用して分類器を構築しており、グリッド検索を実行して最適なモデルの検索を自動化します。ここでは、コードです:IndexErrorを生成するSVMを使用したGridSearch

from sklearn.svm import SVC 
from sklearn.model_selection import train_test_split 
from sklearn.model_selection import GridSearchCV 
from sklearn.multiclass import OneVsRestClassifier 

X.shape  # (22343, 323) 
y.shape  # (22343, 1) 

X_train, X_test, y_train, y_test = train_test_split(
    X, Y, test_size=0.4, random_state=0 
) 

tuned_parameters = [ 
    { 
    'estimator__kernel': ['rbf'], 
    'estimator__gamma': [1e-3, 1e-4], 
    'estimator__C': [1, 10, 100, 1000] 
    }, 
    { 
    'estimator__kernel': ['linear'], 
    'estimator__C': [1, 10, 100, 1000] 
    } 
] 

model_to_set = OneVsRestClassifier(SVC(), n_jobs=-1) 
clf = GridSearchCV(model_to_set, tuned_parameters) 
clf.fit(X_train, y_train) 

は、私は(これは全体のスタックトレースだけで、最後の3回の呼び出しではありません。)は、次のエラーメッセージが表示されます:私は配列を再形成しようとすると、また

---------------------------------------------------- 
/anaconda/lib/python3.5/site-packages/sklearn/model_selection/_split.py in split(self, X, y, groups) 
    88   X, y, groups = indexable(X, y, groups) 
    89   indices = np.arange(_num_samples(X)) 
---> 90   for test_index in self._iter_test_masks(X, y, groups): 
    91    train_index = indices[np.logical_not(test_index)] 
    92    test_index = indices[test_index] 

/anaconda/lib/python3.5/site-packages/sklearn/model_selection/_split.py in _iter_test_masks(self, X, y, groups) 
    606 
    607  def _iter_test_masks(self, X, y=None, groups=None): 
--> 608   test_folds = self._make_test_folds(X, y) 
    609   for i in range(self.n_splits): 
    610    yield test_folds == i 

/anaconda/lib/python3.5/site-packages/sklearn/model_selection/_split.py in _make_test_folds(self, X, y, groups) 
    593   for test_fold_indices, per_cls_splits in enumerate(zip(*per_cls_cvs)): 
    594    for cls, (_, test_split) in zip(unique_y, per_cls_splits): 
--> 595     cls_test_folds = test_folds[y == cls] 
    596     # the test split can be too big because we used 
    597     # KFold(...).split(X[:max(c, n_splits)]) when data is not 100% 

IndexError: too many indices for array 

yが(22343、)であるため、tuned_pa​​rametersをデフォルト値に設定してもGridSearchが終了しないことがわかります。

のPython:3.5.2

scikit学習:0.18

パンダ:0.19.0

+0

サンプル数を減らして実行しようとしましたか? – MMF

答えて

3

それをそれが助け場合は、ここに

とは、すべてのパッケージのためのバージョンです実装にエラーがないようです。

sklearnドキュメントに記載されているように、「適合時間の複雑さは、サンプルの数が10000個のサンプルを超えるデータセットに拡大するのを困難にします。 See documentation here

あなたのケースでは、22343個のサンプルがあり、計算上の問題やメモリの問題が発生する可能性があります。そのため、あなたがあなたのデフォルトの履歴書を作成するときには、多くの時間がかかります。 10000以下のサンプルを使用して列車を減らしてみてください。

関連する問題