Ich denke, Sie sollten diese Multi-Class-Klassifizierung in einer Eins-gegen-den-Rest-Art behandeln (so misst jede 2x2 Tabelle i
die Leistung eines binären Klassifizierungsproblems, ob jedes obs zu Label i
gehört oder nicht). Folglich können Sie TP, FP, FN, TN für jedes einzelne Label berechnen.
import numpy as np
confusion_matrix = np.array([[2,0,3,4],
[0,4,5,1],
[1,0,3,2],
[5,0,0,4]])
def process_cm(confusion_mat, i=0, to_print=True):
# i means which class to choose to do one-vs-the-rest calculation
# rows are actual obs whereas columns are predictions
TP = confusion_mat[i,i] # correctly labeled as i
FP = confusion_mat[:,i].sum() - TP # incorrectly labeled as i
FN = confusion_mat[i,:].sum() - TP # incorrectly labeled as non-i
TN = confusion_mat.sum().sum() - TP - FP - FN
if to_print:
print('TP: {}'.format(TP))
print('FP: {}'.format(FP))
print('FN: {}'.format(FN))
print('TN: {}'.format(TN))
return TP, FP, FN, TN
for i in range(4):
print('Calculating 2x2 contigency table for label{}'.format(i))
process_cm(confusion_matrix, i, to_print=True)
Calculating 2x2 contigency table for label0
TP: 2
FP: 6
FN: 7
TN: 19
Calculating 2x2 contigency table for label1
TP: 4
FP: 0
FN: 6
TN: 24
Calculating 2x2 contigency table for label2
TP: 3
FP: 8
FN: 3
TN: 20
Calculating 2x2 contigency table for label3
TP: 4
FP: 7
FN: 5
TN: 18
Große Antwort! Um der Diskussion ein wenig hinzuzufügen, möchte ich darauf hinweisen, dass "scikit-learn" auch Funktionen für Bewertungskennzahlen in mehreren Klassen bietet. Wenn Sie planen, TP, FP, FN und TN zu einer ROC zu aggregieren, würde ich vorschlagen, die Scoring-Metrik-Methoden zu verwenden, die [hier dokumentiert] sind (http://scikit-learn.org/stable/modules/classes.html) # sklearn-metrics-metrics) und in [das Benutzerhandbuch hier] (http://scikit-learn.org/stable/modules/model_evaluation.html#classification-metrics) angesprochen. Metriken wie F1-Score werden sehr verwirrend, wenn viele Klassen eingeführt werden, daher sind diese Metriken sehr nützlich. – AN6U5