LoginSignup
0
0

More than 1 year has passed since last update.

機械学習の2値分類における評価実装

Last updated at Posted at 2021-07-04

#はじめに
正解率、損失、再現率、適合率、F値の可視化の方法をまとめています。
tensorflowを使って学習をし、モデルを保存した状態での方法を書いています。

評価方法

正解率, 損失

#モデルを作成し評価する
score = model.evaluate(x_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Test loss: 0.2857113599777221
Test accuracy: 0.96397587776184

混同行列(再現率、適合率、F値)

predict = model.predict(x_test)
cm = confusion_matrix(y_test, predict>0.75)
print("confusion_matrix")
print(cm)

recall =cm[0][0] / (cm[0][0] +cm[0][1])
precision = cm[0][0] / (cm[0][0] +cm[1][0])
print("recall:" +str(recall))
print("precision:" +str(precision))

F1_score = 2* (recall * precision)/(recall + precision)
print("F値:" + str(F1_score))
out
confusion_matrix
[[ 4567  40]
 [12 3456]]
recall:0.94117647058824
precision:0.7607361963190186
F値:0.82290322580645157

再現率(Recall),適合率(Precision)

  • 再現率
    正解が0のデータのうち、AIくんが0と答えられた割合のこと

  • 適合率
    AIくんが0と答えたデータのうち、正解していた割合のこと

import matplotlib.pyplot as plt
from sklearn import metrics

precision, recall, thresholds = metrics.precision_recall_curve(y_test, test_predict)

plt.plot(np.append(thresholds, 1), recall, label = 'Recall')
plt.plot(np.append(thresholds, 1), precision, label = 'Precision')
plt.legend()
plt.xlabel('Thresholds')
plt.ylabel('Rate')
plt.grid(True)

##F値
再現率と適合率を合わせて評価する指標
再現率と適合率の確率の平均

recall =cm[0][0] / (cm[0][0] +cm[0][1])
precision = cm[0][0] / (cm[0][0] +cm[1][0])
print(recall)
print(precision)

f1_score = 2* (recall * precision)/(recall + precision)
print(f1_score)

#終わりに
これは自分用のメモで作成しました。雑ですみません。
printの出力の数値は適当です。

ご意見、指摘等ありましたら、びしばし教えていただけると嬉しいです。

0
0
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
0
0