2012-09-16 9 views
28

sto ottenendo il seguente errore quando si eseguono selezione funzione ricorsiva con cross-validation:TypeError: solo gli array interi con un elemento possono essere convertiti in un indice

Traceback (most recent call last): 
    File "/Users/.../srl/main.py", line 32, in <module> 
    argident_sys.train_classifier() 
    File "/Users/.../srl/identification.py", line 194, in train_classifier 
    feat_selector.fit(train_argcands_feats,train_argcands_target) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/feature_selection/rfe.py", line 298, in fit 
    ranking_ = rfe.fit(X[train], y[train]).ranking_ 
TypeError: only integer arrays with one element can be converted to an index 

Il codice che genera l'errore è il seguente :

def train_classifier(self): 

    # Get the argument candidates 
    argcands = self.get_argcands(self.reader) 

    # Extract the necessary features from the argument candidates 
    train_argcands_feats = [] 
    train_argcands_target = [] 

    for argcand in argcands: 
     train_argcands_feats.append(self.extract_features(argcand)) 
     if argcand["info"]["label"] == "NULL": 
      train_argcands_target.append("NULL") 
     else: 
      train_argcands_target.append("ARG") 

    # Transform the features to the format required by the classifier 
    self.feat_vectorizer = DictVectorizer() 
    train_argcands_feats = self.feat_vectorizer.fit_transform(train_argcands_feats) 

    # Transform the target labels to the format required by the classifier 
    self.target_names = list(set(train_argcands_target)) 
    train_argcands_target = [self.target_names.index(target) for target in train_argcands_target] 

    ## Train the appropriate supervised model  

    # Recursive Feature Elimination 
    self.classifier = LogisticRegression() 
    feat_selector = RFECV(estimator=self.classifier, step=1, cv=StratifiedKFold(train_argcands_target, 10)) 

    feat_selector.fit(train_argcands_feats,train_argcands_target) 

    print feat_selector.n_features_ 
    print feat_selector.support_ 
    print feat_selector.ranking_ 
    print feat_selector.cv_scores_ 

    return 

so che dovrei eseguire anche GridSearch per i parametri del classificatore regressione logistica, ma io non credo che sia l'origine dell'errore (o è?).

Devo dire che sto testando con circa 50 funzionalità e quasi tutte sono categoriali (è per questo che uso DictVectorizer per trasformarle in modo appropriato).

Qualsiasi aiuto o guida che potresti darmi è più che benvenuto. Grazie!

EDIT

Ecco alcuni esempi dei dati di allenamento:

train_argcands_feats = [{'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'dado', 'head': u'dado', 'head_postag': u'N'}, {'head_lemma': u'postura', 'head': u'postura', 'head_postag': u'N'}, {'head_lemma': u'maioria', 'head': u'maioria', 'head_postag': u'N'}, {'head_lemma': u'querer', 'head': u'quer', 'head_postag': u'V-FIN'}, {'head_lemma': u'PT', 'head': u'PT', 'head_postag': u'PROP'}, {'head_lemma': u'participar', 'head': u'participando', 'head_postag': u'V-GER'}, {'head_lemma': u'surpreendente', 'head': u'supreendente', 'head_postag': u'ADJ'}, {'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'revelar', 'head': u'revela', 'head_postag': u'V-FIN'}, {'head_lemma': u'recusar', 'head': u'recusando', 'head_postag': u'V-GER'}, {'head_lemma': u'maioria', 'head': u'maioria', 'head_postag': u'N'}, {'head_lemma': u'PT', 'head': u'PT', 'head_postag': u'PROP'}, {'head_lemma': u'participar', 'head': u'participando', 'head_postag': u'V-GER'}, {'head_lemma': u'surpreendente', 'head': u'supreendente', 'head_postag': u'ADJ'}, {'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'revelar', 'head': u'revela', 'head_postag': u'V-FIN'}, {'head_lemma': u'governo', 'head': u'Governo', 'head_postag': u'N'}, {'head_lemma': u'de', 'head': u'de', 'head_postag': u'PRP'}, {'head_lemma': u'governo', 'head': u'Governo', 'head_postag': u'N'}, {'head_lemma': u'recusar', 'head': u'recusando', 'head_postag': u'V-GER'}, {'head_lemma': u'maioria', 'head': u'maioria', 'head_postag': u'N'}, {'head_lemma': u'querer', 'head': u'quer', 'head_postag': u'V-FIN'}, {'head_lemma': u'PT', 'head': u'PT', 'head_postag': u'PROP'}, {'head_lemma': u'surpreendente', 'head': u'supreendente', 'head_postag': u'ADJ'}, {'head_lemma': u'Bras\xedlia', 'head': u'Bras\xedlia', 'head_postag': u'PROP'}, {'head_lemma': u'Pesquisa_Datafolha', 'head': u'Pesquisa_Datafolha', 'head_postag': u'N'}, {'head_lemma': u'revelar', 'head': u'revela', 'head_postag': u'V-FIN'}, {'head_lemma': u'muito', 'head': u'Muitas', 'head_postag': u'PRON-DET'}, {'head_lemma': u'prioridade', 'head': u'prioridades', 'head_postag': u'N'}, {'head_lemma': u'com', 'head': u'com', 'head_postag': u'PRP'}, {'head_lemma': u'prioridade', 'head': u'prioridades', 'head_postag': u'N'}] 

train_argcands_target = ['NULL', 'ARG', 'ARG', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'ARG', 'ARG', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'NULL', 'NULL', 'NULL', 'NULL', 'ARG', 'ARG', 'NULL', 'NULL'] 
+0

In base alla traccia dello stack, il problema è all'interno della chiamata 'feat_selector.fit (train_argcands_feats, train_argcands_target)'. 'RFECV' è una classe che crei o è una libreria? È possibile pubblicare il tuo codice 'RFECV.fit()'? – acattle

+0

@acattle È una libreria di scikit-learn: http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFECV.html – feralvam

+0

@acattle dove lo vedi? – XORcist

risposta

32

ho finalmente avuto modo di risolvere il problema. Due cose doveva essere fatto:

  1. train_argcands_target è una lista e deve essere una matrice NumPy. Sono sorpreso che abbia funzionato molto bene quando ho usato lo stimatore direttamente.
  2. Per qualche motivo (non so perché, ancora), non funziona neanche se utilizzo la matrice sparsa creata da DictVectorizer. Dovevo, "manualmente", trasformare ciascun dizionario di caratteristiche in un array di caratteristiche con solo interi che rappresentavano ciascun valore di funzione. Il processo di trasformazione è simile a quello che presento nel codice per i valori di destinazione.

Grazie a tutti coloro che hanno cercato di aiutare!

9

Se qualcuno è ancora interessato,

ho usato il CountVectorizer su qualcosa di molto simile e che mi ha dato lo stesso errore. Mi sono reso conto che il vectorizer mi dà una matrice sparsa COO che è fondamentalmente una lista di coordinate. Non è possibile accedere agli elementi nelle matrici COO tramite gli indici di riga. È meglio convertirlo in una matrice CSR (Compressed Sparse Row) che indicizza in termini di righe. La conversione può essere effettuata facilmente coo_matrix.tocsr(). Non è richiesto nessun altro cambiamento, questo ha funzionato per me.

+1

Ho avuto lo stesso problema e questa soluzione ha funzionato per me. È più facile che recuperare la risposta accettata qui. Inoltre, il motivo fornito (COO inaccessibile tramite indici di riga) ha molto più senso. – Dexter

+0

Totalmente d'accordo. COO è il problema. –

Problemi correlati