信用模型的交叉验证

Python 信用风险建模

Michael Crabtree

Data Scientist, Ford Motor Company

交叉验证基础

  • 用于以接近新数据的方式训练与测试模型
  • 将训练集分块以估计未来表现
  • 使用 DMatrix,一种为 XGBoost 优化的内部结构
  • 早停:在指标一段轮次无提升时停止
Python 信用风险建模

交叉验证的工作原理

  • 将训练数据分成若干折进行训练,并在未用部分上测试
  • 最终在真实测试集上评估

k 折交叉验证示意图

1 https://scikit-learn.org/stable/modules/cross_validation.html
Python 信用风险建模

在 XGBoost 中设置交叉验证

# Set the number of folds
n_folds = 2
# Set early stopping number
early_stop = 5
# Set any specific parameters for cross validation
params = {'objective': 'binary:logistic',
          'seed': 99, 'eval_metric':'auc'}
  • 'binary':'logistic' 指定 loan_status 的二分类任务
  • 'eval_metric':'auc' 要求 XGBoost 用 AUC 评估表现
Python 信用风险建模

在 XGBoost 中使用交叉验证

# Restructure the train data for xgboost
DTrain = xgb.DMatrix(X_train, label = y_train)
# Perform cross validation
xgb.cv(params, DTrain, num_boost_round = 5, nfold=n_folds,
       early_stopping_rounds=early_stop)
  • DMatrix()xgboost 创建用于训练的优化对象
Python 信用风险建模

交叉验证结果

  • 将交叉验证的数值汇总为数据框

交叉验证得分示例

Python 信用风险建模

交叉验证评分

  • 在 scikit-learn 中用 cross_val_score() 进行交叉验证并打分
# Import the module
from sklearn.model_selection import cross_val_score
# Create a gbt model
xg = xgb.XGBClassifier(learning_rate = 0.4, max_depth = 10)
# Use cross valudation and accuracy scores 5 consecutive times
cross_val_score(gbt, X_train, y_train, cv = 5)
array([0.92748092, 0.92575308, 0.93975392, 0.93378608, 0.93336163])
Python 信用风险建模

Passons à la pratique !

Python 信用风险建模

Preparing Video For Download...