2013-10-28 13 views
7

ho usato seguente serie di codice: e ho bisogno di verificare l'accuratezza dei X_train e X_testPython: Come trovare l'accuratezza dei risultati in SVM Testo Classificatore Algoritmo per Multilabel Classe

Il seguente codice funziona per me nella mia classificazione problema per multi-marcato classe

import numpy as np 
from sklearn.pipeline import Pipeline 
from sklearn.feature_extraction.text import CountVectorizer 
from sklearn.svm import LinearSVC 
from sklearn.feature_extraction.text import TfidfTransformer 
from sklearn.multiclass import OneVsRestClassifier 

X_train = np.array(["new york is a hell of a town", 
        "new york was originally dutch", 
        "the big apple is great", 
        "new york is also called the big apple", 
        "nyc is nice", 
        "people abbreviate new york city as nyc", 
        "the capital of great britain is london", 
        "london is in the uk", 
        "london is in england", 
        "london is in great britain", 
        "it rains a lot in london", 
        "london hosts the british museum", 
        "new york is great and so is london", 
        "i like london better than new york"]) 
y_train = [[0],[0],[0],[0] 
      ,[0],[0],[1],[1] 
      ,[1],[1],[1],[1] 
      ,[2],[2]] 
X_test = np.array(['nice day in nyc', 
        'the capital of great britain is london', 
        'i like london better than new york', 
        ]) 
target_names = ['Class 1', 'Class 2','Class 3'] 

classifier = Pipeline([ 
    ('vectorizer', CountVectorizer(min_df=1,max_df=2)), 
    ('tfidf', TfidfTransformer()), 
    ('clf', OneVsRestClassifier(LinearSVC()))]) 
classifier.fit(X_train, y_train) 
predicted = classifier.predict(X_test) 
for item, labels in zip(X_test, predicted): 
    print '%s => %s' % (item, ', '.join(target_names[x] for x in labels)) 

OUTPUT

nice day in nyc => Class 1 
the capital of great britain is london => Class 2 
i like london better than new york => Class 3 

Vorrei verificare l'accuratezza tra Training and Test Dataset. Score funzione non funziona per me, mostra un errore che indica che il valore non può multietichetta accettato

>>> classifier.score(X_train, X_test) 

NotImplementedError: punteggio non è supportata per classificatori multilabel

aiutarmi a ottenere risultati di precisione per addestramento e dati di test e scegliere un algoritmo per il nostro caso di classificazione.

risposta

9

Se si desidera ottenere un punteggio di precisione per il set di test, è necessario creare un codice di risposta, che è possibile chiamare y_test. Non puoi sapere se le tue previsioni sono corrette a meno che tu non conosca le risposte corrette.

Una volta ottenuto un codice di risposta, è possibile ottenere la precisione. Il metodo desiderato è sklearn.metrics.accuracy_score.

ho scritto qui di seguito:

from sklearn.metrics import accuracy_score 

# ... everything else the same ... 

# create an answer key 
# I hope this is correct! 
y_test = [[1], [2], [3]] 

# same as yours... 
classifier.fit(X_train, y_train) 
predicted = classifier.predict(X_test) 

# get the accuracy 
print accuracy_score(y_test, predicted) 

Inoltre, sklearn ha diversi altri parametri, oltre la precisione. Li vedo qui: sklearn.metrics

+0

Grazie, funziona per il mio problema –

+2

Trovo 'classificatore_rapporto' (da sklearn) molto utile in quanto contiene una tabella con le metriche più frequenti. –