预测违约概率
所有数据处理都已完成,现在可以开始生成违约概率的预测。您将使用 LogisticRegression() 在数据上训练一个模型,并查看它如何给出违约概率。
为更好地理解 predict_proba 的输出,您应当将示例记录与其预测的违约概率放在一起查看。前 5 条预测与真实的 loan_status 相比如何?
数据集 cr_loan_prep 以及 X_train、X_test、y_train 和 y_test 已加载到工作区。
本练习是课程的一部分
Python 信用风险建模
练习说明
- 在训练数据上训练一个逻辑回归模型,并将其保存为
clf_logistic。 - 在测试数据上使用
predict_proba()生成预测,保存到preds。 - 创建两个数据框
preds_df和true_df,分别存放前 5 个预测值和真实的loan_status值。 - 使用
.concat()将true_df和preds_df合并后打印为一组。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Train the logistic regression model on the training data
____ = ____(solver='lbfgs').____(____, np.ravel(____))
# Create predictions of probability for loan status using test data
____ = clf_logistic.____(____)
# Create dataframes of first five predictions, and first five true labels
____ = pd.DataFrame(____[:,1][0:5], columns = ['prob_default'])
____ = y_test.____()
# Concatenate and print the two data frames for comparison
print(pd.____([true_df.reset_index(drop = True), preds_df], axis = 1))