Help us understand the problem. What is going on with this article?

Pythonで混合行列からF値を算出する

More than 1 year has passed since last update.

機械学習のモデル評価で使う「混合行列」から、「F値(F1値)」を算出するスクリプトです。

F値計算モデルの参考サイト:
機械学習で使う指標総まとめ - 多クラス分類

ソースコード

calculation_f1.py
import numpy as np

input_matrix = [ [2, 1, 0],[1, 6, 2],[0, 0, 3] ]

# input_matrix = [ [2, 1, 0], 
#                  [1, 6, 2], 
#                   [0, 0, 3] ]


def calculation_f1(input_matrix):

    confusion_matrix = np.array(input_matrix, dtype = 'float')

    matrix_len = len(confusion_matrix)
    col_sum = np.sum(confusion_matrix, axis=1)
    row_sum = np.sum(confusion_matrix, axis=0)

    F1_list = []

    for i in range(0, matrix_len):        
        Precision = confusion_matrix[i][i] / (confusion_matrix[i][i] + (col_sum[i] - confusion_matrix[i][i]))
        Recall = confusion_matrix[i][i] / (confusion_matrix[i][i] + (row_sum[i] - confusion_matrix[i][i]))
        F1 = (2*Precision*Recall) / (Precision+Recall)
        F1_list.append(F1)

    return sum(F1_list)/matrix_len

print("F value: " + str(calculation_f1(input_matrix)))

実行結果

F value: 0.7222222222222222

scikit-learnでF値のログを取り忘れた為に書きました。
皆さんもログの収集漏れには、くれぐれもお気をつけください...

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした