2013-01-08 5 views
5

che sto facendo una ricerca a griglia sui dati multilabel come segue:GridSearch per Multilabel OneVsRestClassifier?

#imports 
from sklearn.svm import SVC as classifier 
from sklearn.pipeline import Pipeline 
from sklearn.decomposition import RandomizedPCA 
from sklearn.cross_validation import StratifiedKFold 
from sklearn.grid_search import GridSearchCV 

#classifier pipeline 
clf_pipeline = clf_pipeline = OneVsRestClassifier(
       Pipeline([('reduce_dim', RandomizedPCA()), 
          ('clf', classifier()) 
          ] 
         )) 

C_range = 10.0 ** np.arange(-2, 9) 
gamma_range = 10.0 ** np.arange(-5, 4) 
n_components_range = (10, 100, 200) 
degree_range = (1, 2, 3, 4) 

param_grid = dict(estimator__clf__gamma=gamma_range, 
        estimator__clf__c=c_range, 
        estimator__clf__degree=degree_range, 
        estimator__reduce_dim__n_components=n_components_range) 

grid = GridSearchCV(clf_pipeline, param_grid, 
           cv=StratifiedKFold(y=Y, n_folds=3), n_jobs=1, 
           verbose=2) 
grid.fit(X, Y) 

sto vedendo il seguente traceback:

/Users/andrewwinterman/Documents/sparks-honey/classifier/lib/python2.7/site-packages/sklearn/grid_search.pyc in fit_grid_point(X, y, base_clf, clf_params, train, test, loss_func, score_func, verbose, **fit_params) 
    107 
    108  if y is not None: 
--> 109   y_test = y[safe_mask(y, test)] 
    110   y_train = y[safe_mask(y, train)] 
    111   clf.fit(X_train, y_train, **fit_params) 

TypeError: only integer arrays with one element can be converted to an index 

Sembra GridSearchCV oggetti a più etichette. Come dovrei aggirare questo? Devo eseguire iterazioni esplicite attraverso le classi univoche con label_binarizer, eseguendo la ricerca della griglia su ogni sotto-stimatore?

+0

Si sta utilizzando 0.12.1 o 0.13? Penso che il problema dovrebbe andare via quando si aggiorna a 0.13. –

+0

Stavo usando un ramo dev di 0.13. Ci proverò di nuovo a breve. – Maus

+0

Dovrebbe funzionare nel release 0.13 e master corrente. In caso contrario, apri un problema su github. –

risposta

6

Penso che ci sia un bug nel grid_search.py ​​

Hai provato a dare y come numpy array?

import numpy as np 
Y = np.asarray(Y) 
+0

no, in realtà ho smesso di lavorare con scikit. Dovrai provare tu stesso le soluzioni proposte. Se puoi provare che uno funziona, lo accetto :) – Maus

+1

ho appena risolto il mio problema, grazie. –

+0

@ ZéRicardo, felice di sentirlo :) – Thorn

Problemi correlati