预测违约概率

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]])

使用逻辑回归计算违约概率的公式

# Calculating probability of default
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 encoding)

  • 用数字表示字符串

数据中的贷款意图示例

Python 信用风险建模

独热编码(One-hot encoding)

  • 用数字表示字符串
  • 在新列 column_VALUE 中填 01

对贷款意图列进行独热编码的示例

Python 信用风险建模

使用 get_dummies

  • pandas 中使用 get_dummies()
# Separate the numeric columns
cred_num = cr_loan.select_dtypes(exclude=['object'])
# Separate non-numeric columns
cred_cat = cr_loan.select_dtypes(include=['object'])
# One-hot encode the non-numeric columns only
cred_cat_onehot = pd.get_dummies(cred_cat)
# Union the numeric columns with the one-hot encoded columns
cr_loan = pd.concat([cred_num, cred_cat_onehot], axis=1)
Python 信用风险建模

预测未来的概率

  • 使用 scikit-learn 的 .predict_proba() 方法
# Train the model
clf_logistic.fit(X_train, np.ravel(y_train))
# Predict using the model
clf_logistic.predict_proba(X_test)
  • 生成违约概率数组
# Probabilities: [[non-default, default]]
array([[0.55, 0.45]])
Python 信用风险建模

让我们来练习!

Python 信用风险建模

Preparing Video For Download...