1) sklearn의 classification_report을 사용하기
- 실제값과 예측값을 받아서 각 클래스 별로 평가지표를 계산한 뒤 출력해 준다.
- support: 각 클래스 라벨의 실제 갯수
- macro avg: 각 클래스 별로 동일한 가중치를 부여하는 단순 평균
- weighted avg: 각 클래스에 속하는 표본의 갯수를 이용한 가중 평균
- 0: class = 0이 Positive인 경우의 지표
- 1: class = 1이 Positive인 경우의 지표
from sklearn.metrics import classification_report
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X, y = make_classification(n_samples=1000, n_classes=2, weights=[0.2, 0.8], random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = LogisticRegression(random_state=0).fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))
"""
precision recall f1-score support
0 0.91 0.89 0.90 44
1 0.98 0.98 0.98 206
accuracy 0.96 250
macro avg 0.94 0.93 0.94 250
weighted avg 0.96 0.96 0.96 250
"""
2) cross_validate를 사용하는 경우
def scores_cv(model, x, y):
"""
model: model to use
x: features
y: output
"""
score_list = ['accuracy', 'precision', 'recall', 'f1']
scores = cross_validate(model, x, y, scoring = score_list, cv = 5) # default = stratifiedKfold
scores_df = pd.DataFrame(scores)
return pd.concat([scores_df, scores_df.apply(['mean', 'std'])])
3) neural network 모델을 사용하는 경우, 직접 confusion matrix 계산을 해주어야 함
# for neural network
def cm_to_metrics(cm:np.array) -> tuple:
'''Confusion Matrix to Metrics
cm: confusion matrix of shape (2,2)
'''
(tn, fp), (fn, tp) = cm
accuracy = (tp+tn)/(tp+tn+fn+fp)
precision = tp/(tp+fp)
recall = tp/(tp+fn)
f1 = 2*(precision*recall)/(precision+recall)
return (accuracy, precision, recall, f1)
from metrics import cm_to_metrics
def bcm_results(confusion_results):
'''
confusion_results: 학습&평가할 때 미리 저장해주어야 함.
예)
'''
scores = {
'accuracy': [],
'precision': [],
'recall': [],
'f1': []
}
for bcm in confusion_results:
bcm.plot()
plt.show()
cm = bcm.compute().cpu().numpy()
accuracy, precision, recall, f1 = cm_to_metrics(cm)
scores['accuracy'].append(accuracy)
scores['precision'].append(precision)
scores['recall'].append(recall)
scores['f1'].append(f1)
return scores
"""Visualization with confusion matrix"""
from metrics import bcm_results, cm_to_metrics
scores = bcm_results(confusion_results)
scores_df = pd.DataFrame(scores)
pd.concat([scores_df, scores_df.apply(['mean', 'std'])])