정밀도와 재현율을 결합한 지표. 정밀도와 재현율이 어느 한쪽으로 치우치지 않을 때 높은 값을 갖는다.
사이킷런에서는 f1_score() API를 제공한다.
from sklearn.metrics import f1_score
f1 = f1_score(y_test,pred)
print('F1 스코어: {0:.4f}'.format(f1))
Python
복사
임계값을 변화시키면서 정확도, 정밀도, 재현율, f1 스코어를 살펴보자.
# 임계값 변화시키면서 정밀도, 재현율, f1 score 알아보기
from sklearn.preprocessing import Binarizer
# f1 score계산하는 로직 넣어주기
def get_clf_eval(y_test,pred):
confusion = confusion_matrix(y_test,pred)
accuracy = accuracy_score(y_test,pred)
precision = precision_score(y_test,pred)
recall = recall_score(y_test,pred)
f1 = f1_score(y_test,pred)
print('오차 행렬')
print(confusion)
print('정확도:{0:.4f}, 정밀도:{1:.4f}, 재현율:{2:.4f}, f1 스코어:{3:.4f}'.format(accuracy,precision,recall,f1))
thresholds = [0.4,0.45,0.5,0.55,0.6]
def get_eval_by_threshold(y_test,pred_proba_c1,thresholds):
# thresholds list객체 내의 값을 차례로 iteration하면서 evaluation수행
for custom_threshold in thresholds:
binarizer = Binarizer(threshold=custom_threshold).fit(pred_proba_c1)
custom_predict = binarizer.transform(pred_proba_c1)
print('임계값:',custom_threshold)
get_clf_eval(y_test,custom_predict)
thresholds = [0.4, 0.45,0.5,0.55,0.6]
pred_proba = lr_clf.predict_proba(X_test)
get_eval_by_threshold(y_test,pred_proba[:,1].reshape(-1,1),thresholds)
'''
임계값: 0.4
오차 행렬
[[98 20]
[10 51]]
정확도:0.8324, 정밀도:0.7183, 재현율:0.8361, f1 스코어:0.7727
임계값: 0.45
오차 행렬
[[103 15]
[ 12 49]]
정확도:0.8492, 정밀도:0.7656, 재현율:0.8033, f1 스코어:0.7840
임계값: 0.5
오차 행렬
[[104 14]
[ 13 48]]
정확도:0.8492, 정밀도:0.7742, 재현율:0.7869, f1 스코어:0.7805
임계값: 0.55
오차 행렬
[[109 9]
[ 15 46]]
정확도:0.8659, 정밀도:0.8364, 재현율:0.7541, f1 스코어:0.7931
임계값: 0.6
오차 행렬
[[112 6]
[ 16 45]]
정확도:0.8771, 정밀도:0.8824, 재현율:0.7377, f1 스코어:0.8036
'''
Python
복사
f1스코어의 측면에서는 임계값이 0.6일 때 가장 좋은 값이 나오고 있다. 하지만, 재현율이 가장 작다는 점을 주의해야 한다.