开始使用免费开始使用

创建训练集和测试集

您刚刚在不同的列上训练了 LogisticRegression() 模型。

您知道应当将数据划分为训练集和测试集。可以使用 test_train_split() 同时创建二者。训练集用于训练模型并生成预测,测试集用于评估模型效果。如果不评估模型,您就无法判断它在新的贷款数据上能否表现良好。

除了作为模型属性的 intercept_LogisticRegression() 模型还有 .coef_ 属性。它用于展示每个训练列(特征)对违约概率预测的重要性。

数据集 cr_loan_clean 已加载到工作区。

本练习是课程的一部分

Python 信用风险建模

查看课程

练习说明

  • 使用利率、工作年限和收入创建数据集 X。使用贷款状态创建数据集 y
  • 使用 train_test_split()Xy 划分为训练集和测试集。
  • 创建并训练一个 LogisticRegression() 模型,保存为 clf_logistic
  • 使用 .coef_ 打印模型的系数。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create the X and y data sets
X = ____[[____,____,____]]
y = ____[[____]]

# Use test_train_split to create the training and test sets
X_train, X_test, y_train, y_test = ____(____, ____, test_size=.4, random_state=123)

# Create and fit the logistic regression model
____ = ____(solver='lbfgs').____(____, np.ravel(____))

# Print the models coefficients
print(____.coef_)
编辑并运行代码