u++の備忘録

言語処理100本ノック 2020「56. 適合率,再現率,F1スコアの計測」

問題文

nlp100.github.io

問題の概要

適合率,再現率,F1スコアはそれぞれ「precision_score()」「recall_score()」「f1_score()」で計算できます。「average」には「'micro'」「'macro'」などが指定可能*1です。

import pandas as pd
import joblib
from sklearn.metrics import recall_score, precision_score, f1_score


X_train = pd.read_table('ch06/train.feature.txt', header=None)
X_test = pd.read_table('ch06/test.feature.txt', header=None)
y_train = pd.read_table('ch06/train.txt', header=None)[1]
y_test = pd.read_table('ch06/test.txt', header=None)[1]

clf = joblib.load('ch06/model.joblib')
y_pred = clf.predict(X_test)

print(f'test recall of None: {recall_score(y_test, y_pred, average=None)}')
print(f'test recall of micro: {recall_score(y_test, y_pred, average="micro")}')
print(f'test recall of macro: {recall_score(y_test, y_pred, average="macro")}')
print(f'test precision of None: {precision_score(y_test, y_pred, average=None)}')
print(f'test precision of micro: {precision_score(y_test, y_pred, average="micro")}')
print(f'test precision of macro: {precision_score(y_test, y_pred, average="macro")}')
print(f'test f1 of None: {f1_score(y_test, y_pred, average=None)}')
print(f'test f1 of micro: {f1_score(y_test, y_pred, average="micro")}')
print(f'test f1 of macro: {f1_score(y_test, y_pred, average="macro")}')