さまざまな性能評価指標
混合行列
Coding
code: Python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# yのカテゴリ変数「M」「B」を数値に変換する
le = LabelEncoder()
y = le.fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=1)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
pipe_svc = make_pipeline(StandardScaler(), SVC(random_state=1))
pipe_svc.fit(X_train, y_train)
y_pred = pipe_svc.predict(X_test)
# テストと予測のデータから混合行列を生成
confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)
print(confmat)
%matplotlib inline
import matplotlib.pyplot as plt
# 図のサイズを指定
fig, ax = plt.subplots(figsize=(2.5, 2.5))
# matshow関数で行列からヒートマップを描画
ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat.shape0): # クラス0の繰り返し処理 for j in range(confmat.shape1): # クラス1の繰り返し処理 ax.text(x=j, y=i, s=confmati, j, va='center', ha='center') # 件数を表示 plt.xlabel('predicted label')
plt.ylabel('true label')
plt.tight_layout()
plt.show()
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
https://gyazo.com/6eaf3869293cc3e55c3f991abdfbd6aa
適合率、再現率、F1スコアを出力
Coding
code: Python
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score, f1_score
print('Precision: {:.3f}'.format(precision_score(y_true=y_test, y_pred=y_pred)))
print('Recall: {:.3f}'.format(recall_score(y_true=y_test, y_pred=y_pred)))
print('F1: {:.3f}'.format(f1_score(y_true=y_test, y_pred=y_pred)))
-----------------------------------------------------------------------------------
Precision: 0.976
Recall: 0.952
F1: 0.964
-----------------------------------------------------------------------------------