Machine Learning/이론
f1-score 매개변수 average의 종류
파송송
2024. 6. 17. 15:08
728x90
scikit-learn에서는 matrix 계산을 위해 f1-score를 제공한다.
from sklearn import metrics
f1_score = metrics.f1_score(targets, pred, average='')
Sklearn 문서에 의하면 각 정의는 다음과 같다.
- macro average - averaging the unweighted mean per label( label별 산술 평균값)
- 모든 F1를 평균 낸 것
- weighted average - averaging the support-weighted mean per label (label 별 샘플 수의 비중 가중 평균값)
- label의 개수에 따라 가중치를 부여한 것
- micro average - averaging the total true positives, false negatives and false positives ( TP, FN, FP 평균값 다중 레이블 분류에만 해당)
- label 구별 없이 TP, FP, FN의 통합으로 f1을 매긴 것
모든 라벨이 중요하다면 macro, 샘플이 많은 라벨이 중요하다면 Weighted, 라벨 구분 없이 전체적 성능이 중요하다면 micro를 사용하면 된다.
TP = 해당 label을 label이라고 바르게 예측한 것 (True Positive)
FP = 해당 label이 아닌 것을 label로 예측한 것 (False Positive) / label 0의 FP를 구할 때, 0으로 예측했으나(positive), 틀림(False)
FN = 해당 label을 label이 아닌 것으로 예측한 것(False Negative) / label 0의 FN를 구할 때, 0으로 예측해야 하는데 (Negative) 0으로 예측 안 함(False)
(Positive, Negative를 해당 label로 하여 이해하는 것이 잘 외워짐 )
from sklearn.metrics import classification_report
from sklearn import metrics
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ['class 0', 'class 1', 'class 2']
print(classification_report(y_true, y_pred, target_names=target_names))
micro_f1 = metrics.f1_score(y_pred, y_true, average='micro')
print(f"F1 Score (Micro) : {micro_f1}")
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html
728x90