Ich habe eine Multiclass-Klassifizierungsaufgabe. Wenn ich mein Skript anhand des scikit-Beispiels wie folgt ausführe:
classifier = OneVsRestClassifier(GradientBoostingClassifier(n_estimators=70, max_depth=3, learning_rate=.02))
y_pred = classifier.fit(X_train, y_train).predict(X_test)
cnf_matrix = confusion_matrix(y_test, y_pred)
Ich erhalte diesen Fehler:
File "C:\ProgramData\Anaconda2\lib\site-packages\sklearn\metrics\classification.py", line 242, in confusion_matrix
raise ValueError("%s is not supported" % y_type)
ValueError: multilabel-indicator is not supported
Ich habe versucht, den labels=classifier.classes_
an confusion_matrix()
zu übergeben, aber es hilft nicht.
y_test und y_pred sind wie folgt:
y_test =
array([[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0],
...,
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0]])
y_pred =
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
...,
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0]])
Zuerst müssen Sie das Label-Ausgabearray erstellen Nehmen wir an, Sie haben 3 Klassen: 'cat', 'dog', 'house' indexiert: 0,1,2 . Und die Vorhersage für 2 Beispiele lautet: "Hund", "Haus". Ihre Ausgabe wird sein:
y_pred = [[0, 1, 0],[0, 0, 1]]
führen Sie y_pred.argmax (1) aus, um Folgendes zu erhalten: [1,2] Dieses Array steht für die ursprünglichen Labelindizes, dh für: ['hund', 'haus']
num_classes = 3
# from lable to categorial
y_prediction = np.array([1,2])
y_categorial = np_utils.to_categorical(y_prediction, num_classes)
# from categorial to lable indexing
y_pred = y_categorial.argmax(1)
Das hat für mich funktioniert:
y_test_non_category = [ np.argmax(t) for t in y_test ]
y_predict_non_category = [ np.argmax(t) for t in y_predict ]
from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test_non_category, y_predict_non_category)
dabei sind y_test
und y_predict
kategoriale Variablen wie One-Hot-Vektoren.
Ich habe gerade die Ausgaben y_test
-Matrix von der Vorhersage y_pred
-Matrix abgezogen, während das kategoriale Format beibehalten wurde. Im Fall von -1
nahm ich ein falsch negatives an, während im Fall von 1
ein falsch positives Ergebnis war.
Nächster:
if output_matrix[i,j] == 1 and predictions_matrix[i,j] == 1:
produced_matrix[i,j] = 2
Am Ende mit der folgenden Notation:
Schließlich können Sie einige naive Zählungen durchführen, um jede Verwirrungsmetrik zu erzeugen.