預測違約機率

以 Python 進行信用風險建模

Michael Crabtree

Data Scientist, Ford Motor Company

羅吉斯回歸係數

# Model Intercept
array([-3.30582292e-10])
# Coefficients for ['loan_int_rate','person_emp_length','person_income']
array([[ 1.28517496e-09, -2.27622202e-09, -2.17211991e-05]])

使用羅吉斯回歸計算違約機率的公式

# 計算違約機率
int_coef_sum = -3.3e-10 +
    (1.29e-09 * loan_int_rate) + (-2.28e-09 * person_emp_length) + (-2.17e-05 * person_income)
prob_default = 1 / (1 + np.exp(-int_coef_sum))
prob_nondefault = 1 - (1 / (1 + np.exp(-int_coef_sum)))
以 Python 進行信用風險建模

解讀係數

# Intercept
intercept = -1.02
# Coefficient for employment length
person_emp_length_coef = -0.056
  • person_emp_length 每增加 1 年,違約機率會下降
以 Python 進行信用風險建模

解讀係數

# Intercept
intercept = -1.02
# Coefficient for employment length
person_emp_length_coef = -0.056
  • person_emp_length 每增加 1 年,違約機率會下降
intercept person_emp_length value * coef probability of default
-1.02 10 (10 * -0.06) .17
-1.02 11 (11 * -0.06) .16
-1.02 12 (12 * -0.06) .15
以 Python 進行信用風險建模

使用非數值欄位

  • 數值型:loan_int_rateperson_emp_lengthperson_income

  • 非數值型:

    cr_loan_clean['loan_intent']
    
EDUCATION            
MEDICAL              
VENTURE              
PERSONAL             
DEBTCONSOLIDATION   
HOMEIMPROVEMENT
  • 若不先處理,在 Python 的機器學習模型中會造成錯誤
以 Python 進行信用風險建模

One-hot 編碼

  • 以數字表示字串

資料中的貸款用途範例

以 Python 進行信用風險建模

One-hot 編碼

  • 以數字表示字串
  • 在新欄位 column_VALUE 中放 01

將貸款用途欄位做 one-hot 編碼的範例

以 Python 進行信用風險建模

Get dummies

  • 使用 pandasget_dummies()
# 分開數值欄位
cred_num = cr_loan.select_dtypes(exclude=['object'])
# 分開非數值欄位
cred_cat = cr_loan.select_dtypes(include=['object'])
# 僅對非數值欄位做 one-hot 編碼
cred_cat_onehot = pd.get_dummies(cred_cat)
# 合併數值欄位與 one-hot 欄位
cr_loan = pd.concat([cred_num, cred_cat_onehot], axis=1)
以 Python 進行信用風險建模

預測未來(機率版)

  • 使用 scikit-learn 的 .predict_proba() 方法
# 訓練模型
clf_logistic.fit(X_train, np.ravel(y_train))
# 用模型進行預測
clf_logistic.predict_proba(X_test)
  • 產生違約機率的陣列
# 機率: [[non-default, default]]
array([[0.55, 0.45]])
以 Python 進行信用風險建模

一起來練習吧!

以 Python 進行信用風險建模

Preparing Video For Download...