3
1

More than 3 years have passed since last update.

sklearn.metrics.ConfusionMatrixDisplayを使った混合行列の可視化

Last updated at Posted at 2020-08-27

scikit-learnを利用すると簡単に混合行列の可視化は可能だが、sklearn.metrics.plot_confusion_matrixestimatorが引数に必要になる。可視化するだけなのでestimatorが不要な方法はないかと調べていたら sklearn.metrics.ConfusionMatrixDisplay が見つかったので簡単にコードを書いてみた。


import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

data = load_breast_cancer()
X, y = data.data, data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = SVC(random_state=0)
clf.fit(X_train, y_train)

y_pred = clf.predict(X_test)
cm = confusion_matrix(y_pred=y_pred, y_true=y_test)
cmp = ConfusionMatrixDisplay(cm, display_labels=data.target_names)

cmp.plot(cmap=plt.cm.Blues)

結果はこんな感じ。
ダウンロード.png

参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1