Ich habe eine Konfusionsmatrix, die ich plotten möchte anstatt nur zu drucken und ich nahm den Code von here.Etikett druckt nicht in einer Matplotlib-Figur
Hier ist die Funktion, die ich mit einigen sehr geringen Änderungen nahm.
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float')/cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
thresh = cm.max()/2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, round(cm[i, j],4)*100,
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
Wenn ich versuche, um die Figur zu zeichnen mit plot_confusion_matrix(cm, classes=['Front', 'Left', 'Rear', 'Right'],normalize=True,title='Confusion matrix, without normalization')
Wie Sie sehen können, um den Wert für die vorhergesagte und wahre Label für links fehlt. Wenn ich dies ohne Normalisierung versuche, scheint alles zu funktionieren und ich kann die tatsächliche Anzahl der vorhergesagten vs wahren Label sehen.
Ich benutze Python 3.5.3 und laufe den Code auf Jupyter Notebook 5.0.0. Was könnte dieses Problem verursachen?
EDIT
Der cm (Konfusionsmatrix) vor dem Funktionsaufruf
cm = np.array([[20633, 219, 357, 118],
[ 136, 340, 199, 0],
[ 49, 10, 15536, 67],
[ 270, 2, 196, 353]])
plot_confusion_matrix(cm, classes=['Front', 'Left', 'Rear', 'Right'],normalize=True,title='Confusion matrix, without normalization')
Das Problem ist, dass dies nicht reproduzierbar ist. Wie kann jemand wissen, warum dieser Wert fehlt, ohne ein Problem zu haben? – ImportanceOfBeingErnest
Vielleicht ist die linke/linke Kombination auf NaN eingestellt? Wie @ImportanceOfBeingErnest sagt, ohne den eigentlichen Aufruf Ihrer Funktion mit Argumenten (Daten, Parameter) ist es unmöglich zu wissen, warum es dieses Verhalten hat. – jlandercy
Bearbeitete die Frage, um die Kommentare zu reflektieren. Ich hoffe jetzt ist es in Ordnung –