二値分類問題の閾値の決め方

 

1. ROC曲線とYouden's J統計量

ROC(Receiver Operating Characteristic)曲線をプロットし、Youden's J統計量(感度 + 特異度 - 1)が最大になる点を閾値として選ぶ方法です。

  1. ROC曲線をプロットする。
  2. 各閾値での感度(True Positive Rate)と特異度(True Negative Rate)を計算する。
  3. Youden's J統計量(感度 + 特異度 - 1)が最大になる閾値を選ぶ。
python
from sklearn.metrics import roc_curve # モデルの予測確率 y_pred_prob = model.predict_proba(X_test)[:, 1] # ROC曲線を計算 fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob) # Youden's J統計量を計算 j_scores = tpr - fpr optimal_threshold = thresholds[np.argmax(j_scores)]

2. F1スコアの最適化

F1スコアはPrecision(適合率)とRecall(再現率)の調和平均であり、クラスのバランスが取れていない場合に特に有効です。F1スコアを最大化する閾値を選びます。

  1. 各閾値でのPrecisionとRecallを計算する。
  2. F1スコアを計算し、最大化する閾値を選ぶ。
python
from sklearn.metrics import precision_recall_curve, f1_score # モデルの予測確率 y_pred_prob = model.predict_proba(X_test)[:, 1] # Precision-Recall曲線を計算 precisions, recalls, thresholds = precision_recall_curve(y_test, y_pred_prob) # F1スコアを計算 f1_scores = 2 * (precisions * recalls) / (precisions + recalls) optimal_threshold = thresholds[np.argmax(f1_scores)]

3. カスタム評価指標を用いた最適化

特定のビジネス要件に応じて、カスタム評価指標を定義し、その指標を最大化する閾値を選ぶ方法です。

python
def custom_metric(y_true, y_pred_prob, threshold): y_pred = (y_pred_prob >= threshold).astype(int) # カスタム評価指標の計算 # 例: TP, TN, FP, FNを使ったスコア tp = np.sum((y_true == 1) & (y_pred == 1)) tn = np.sum((y_true == 0) & (y_pred == 0)) fp = np.sum((y_true == 0) & (y_pred == 1)) fn = np.sum((y_true == 1) & (y_pred == 0)) score = tp - fp # 例としてTPとFPの差を評価指標とする return score # 最適化 best_threshold = 0 best_score = -np.inf for threshold in np.linspace(0, 1, 100): score = custom_metric(y_test, y_pred_prob, threshold) if score > best_score: best_score = score best_threshold = threshold

4. グリッドサーチによる閾値最適化

閾値の範囲を細かく分割し、それぞれの閾値での評価指標を計算して最適な閾値を見つける方法です。

python
thresholds = np.linspace(0, 1, 100) best_threshold = 0 best_score = -np.inf for threshold in thresholds: y_pred = (y_pred_prob >= threshold).astype(int) score = f1_score(y_test, y_pred) # 例としてF1スコアを使用 if score > best_score: best_score = score best_threshold = threshold

これらの手法を活用することで、LightGBMを用いた二値分類モデルの閾値を最適化し、モデルの精度を向上させることができます。具体的な手法の選択は、目的や評価指標に応じて適宜決定してください。

コメント

このブログの人気の投稿

【論文】ChatGPT Prompt Patterns for Improving Code Quality, Refactoring, Requirements Elicitation, and Software Design

【論文】2023 Large Language Models for Software Engineering: Survey and Open Problems

参考論文:コードクローン検出において比較する候補の絞り方