閾值如何影響效能
將閾值設為 0.4,在模型評估上看起來不錯。現在你可以用分類報告(透過 precision_recall_fscore_support() 函式)中的違約召回率來評估財務影響。
為此,你將用違約召回率來估計「預期外損失」:找出在新閾值下你「沒有」攔到的違約比例。這會是一個金額,代表如果所有未被偵測到的違約案件同時違約,你會承受多少損失。
平均貸款金額 avg_loan_amnt 已經計算好,並與 preds_df 與 y_test 一起提供在工作區中。
本練習屬於課程
以 Python 進行信用風險建模
練習說明
- 使用閾值
0.4重新指定loan_status的值。 - 在
preds_df中以.value_counts()取得違約筆數,選取計數結果中的第二個值,並存成num_defaults。 - 從分類矩陣取得違約的召回率,並存成
default_recall。 - 用新的違約召回率估計預期外損失:以
1 - default_recall乘上平均貸款金額與違約貸款筆數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Reassign the values of loan status based on the new threshold
____[____] = ____[____].____(lambda x: 1 if x > ____ else 0)
# Store the number of loan defaults from the prediction data
____ = preds_df[____].____()[1]
# Store the default recall from the classification report
____ = ____(____,preds_df[____])[1][1]
# Calculate the estimated impact of the new default recall rate
print(____ * ____ * (1 - ____))