2016-05-19 1 views
1

Ich benutze die unten, um Ausgabe von Schätzer.Verbessere Python SKLearn CrossValidation Ausgabe

Gibt es einen schnelleren Weg zur Validierung anhand von Scores?

for clfx, label in zip([clf0], ['Random Forest']): 
     scores = cross_validation.cross_val_score(clfx, X, y, cv=5, scoring='accuracy') 
     print "Accuracy : %0.3f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label) 
     scores = cross_validation.cross_val_score(clfx, X, y, cv=5, scoring='precision') 
     print "Precision: %0.3f (+/- %0.2f) [%s] " % (scores.mean(), scores.std(), label) 
     scores = cross_validation.cross_val_score(clfx, X, y, cv=5, scoring='recall') 
     print "Recall : %0.3f (+/- %0.2f) [%s] \n" % (scores.mean(), scores.std(), label) 

Ausgang:

Accuracy : 0.82 (+/- 0.00) [Random Forest] 
Precision: 0.50 (+/- 0.02) [Random Forest] 
Recall : 0.13 (+/- 0.01) [Random Forest] 

Ist das übertrieben und ich sollte die Verwirrung Matrix aus einem Lauf verwenden?

Antwort

1

Leider denken, wenn Sie Metriken kombinieren möchte ich Sie „von Hand“ haben, um die Kreuzvalidierung Iterationen laufen:

from sklearn.metrics import precision_score, accuracy_score, recall_score 
from sklearn.cross_validation import KFold 

all_scores = {'precision':[], 'recall':[], 'accuracy': []} 
for train, test in KFold(n = len(X)): 
    clfx.fit(X[train, :],y[train]) 
    y_pred = clfx.predict(X[test]) 
    all_scores['precision'] += precision_score(y_pred, y[test]) 
    all_scores['accuracy'] += accuracy_score(y_pred, y[test]) 
    all_scores['recall'] += recall_score(y_pred, y[test]) 

scores = all_scores['accuracy'] 
print ("Accuracy : %0.3f (+/- %0.2f) [%s]" % (np.mean(scores), np.std(scores), label)) 
scores = all_scores['precision'] 
print ("Precision: %0.3f (+/- %0.2f) [%s] " % (np.mean(scores), np.std(scores), label)) 
scores = all_scores['recall'] 
print ("Recall : %0.3f (+/- %0.2f) [%s] \n" % (np.mean(scores), np.std(scores), label)) 

Wenn Sie möchten, dass Sie auch multiprocess dies parallelisieren verwenden könnte (das ist ein die wichtigsten Vorteile der Verwendung des scikit-Learn Kreuzvalidierung Funktionen):

from multiprocessing import Pool 

def score(cv_split, clfx=clfx, X=X, y=y): 
    train, test = cv_split 
    clfx.fit(X[train, :],y[train]) 
    y_pred = clfx.predict(X[test]) 
    all_scores = {} 
    all_scores['precision'] = precision_score(y_pred, y[test]) 
    all_scores['accuracy'] = accuracy_score(y_pred, y[test]) 
    all_scores['recall'] = recall_score(y_pred, y[test]) 
    return all_scores 

p = Pool(6) 
scores_by_run = p.map(score, KFold(len(X))) 
all_scores = {k:[d[k] for d in scores_by_run] for k in scores_by_run[0].keys()} 
+0

sicher, ich habe Multiprozessing – maxymoo

+0

zu verwenden, neu geschrieben ich bin verwirrt durch Code. Können Sie bitte einzelne Prozesse verlassen und multi-process ans einschließen. Ich kann nicht herausfinden, wie man etwas wie: zip ([clf0], ['Random Forest']) für clfx verwendet. Ich kann den Einstiegspunkt nicht sehen. – Merlin

+0

ok ich habe die ursprüngliche Antwort auch – maxymoo