임계값 결정

김석범·2023년 7월 10일
0

machine-learing-guide

목록 보기
2/2

preddict_proba()

pred_proba = lr_clf.predict_proba(X_test)

predict_proba 함수의 반환값인 결정 확률을 보고 설정된 임계값을 기준으로 predict결정 (default값은 0.5)

하지만 앞에서 봤듯 임계값을 획일적으로 0.5로 설정하면 불균형 데이터의 경우에서는 재현율과 정밀도가 매우 떨어지는 결과가 나옴.

따라서 데이터의 분포에 따른 적절한 임계값 설정이 중요함.

Binarizer()

from sklearn.preprocessing import Binarizer

custom_threshold = 0.5
pred_proba_1 = pred_proba[:,1].reshape(-1,1)

custom_predict = Binarizer(threshold=custom_threshold).fit_transform(pred_proba_1)

get_clf_eval(y_test, custom_predict)

threshholds = [0.4, 0.45, 0.5, 0.55, 0.6]

def get_eval_by_threshold(y_test, pred_proba_c1, threshholds):
    for custom_threshold in threshholds:
        custom_predict = Binarizer(threshold=custom_threshold).fit_transform(pred_proba_c1)
        print('임계값: ', custom_threshold)
        get_clf_eval(y_test, custom_predict)

get_eval_by_threshold(y_test, pred_proba[:,1].reshape(-1,1), threshholds)
profile
코딩왕

0개의 댓글