sklearn にて、適合率と再現率

以下の投稿で load したX_train, y_train,... を利用。

kidnohr.hatenadiary.com

適合率と再現率と F1 スコア

適合率(PRE)と再現率(REC)について、F1 スコアという性能指標が存在する。

PRE = TP / ( TP + FP )

REC = TP / ( TP + FN )

f1 = 2 * ( PRE * REC ) / ( PRE + REC )

from sklearn.metrics import (
    precision_score,
    recall_score,
    f1_score,
)
print('Precision : %.3f' % precision_score(y_true=y_test, y_pred=y_pred))
print('Recall: %.3f' % recall_score(y_true=y_test, y_pred=y_pred))
print('F1 : %.3f' % f1_score(y_true=y_test, y_pred=y_pred))
Precision : 0.974
Recall: 0.905
F1 : 0.938

F1 スコアで、グリッドサーチの結果を計測

from sklearn.metrics import make_scorer

## pos_label : 陽性のラベル指定
scorer = make_scorer(f1_score, pos_label=0)

gs = GridSearchCV(
    estimator=pipe_svc,
    param_grid=param_grid,
    scoring=scorer,
    cv=10,
    n_jobs=-1
)
gs.fit(X_train, y_train)
print(gs.best_score_)
print(gs.best_params_)
0.9880219137963148
{'svc__C': 100.0, 'svc__gamma': 0.001, 'svc__kernel': 'rbf'}