今日私は私の分類モデルから混乱行列をプロットしようとしました。多くの文字列ラベルを持つMatplotlib matshow
一部のページで検索したところ、matshow
からpyplot
までお手伝いできます。
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues, labels=None):
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
plt.title(title)
fig.colorbar(cax)
if labels:
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()
それは私が、私は多くのラベルを持っている場合でも、一部のラベルが表示されませんいくつかのラベル
y_true = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'c', 'd', 'b', 'a', 'b', 'a']
y_pred = ['a', 'b', 'c', 'd', 'a', 'b', 'b', 'a', 'c', 'a', 'a', 'a', 'a', 'a']
labels = list(set(y_true))
cm = confusion_matrix(y_true, y_pred)
plot_confusion_matrix(cm, labels=labels)
を持っている場合はうまく動作正しく
y_true = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
y_pred = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
labels = list(set(y_true))
cm = confusion_matrix(y_true, y_pred)
plot_confusion_matrix(cm, labels=labels)
私の質問はどのように私はmatshowプロットですべてのラベルを表示できますか?私はfontdict
のようなものを試しましたが、それでも動作しません。
トムありがとう。別の質問、あなたはmatshowプロットで混乱行列の価値を引き出す方法を知っていますか? –
あなたが望むものの詳細について新しい質問をする方がいいでしょう – tom