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

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

More than 1 year has passed since last update.

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