开始使用免费开始使用

限制样本量

在决策树中,另一种防止过拟合的方法是为叶子(或节点)的生长设定所需的最小观测数。

在本练习中,您将:

  • 将该最小阈值设为 100
  • 将新模型拟合到员工数据
  • 在训练集和测试集上检查预测结果

变量 features_traintarget_trainfeatures_testtarget_test 已在您的工作区中可用。

本练习是课程的一部分

HR Analytics:用 Python 预测员工流失

查看课程

练习说明

  • 初始化 DecisionTreeClassifier,并将叶节点的最小观测数设为 100
  • 将决策树模型拟合到训练数据。
  • 检查在训练集和测试集上的预测准确率。

交互式实操练习

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

# Initialize the DecisionTreeClassifier while limiting the sample size in leaves to 100
model_sample_100 = DecisionTreeClassifier(____, random_state=42)

# Fit the model
____.fit(features_train,____)

# Print the accuracy of the prediction (in percentage points) for the training set
print(____.score(features_train,target_train)*100)

# Print the accuracy of the prediction (in percentage points) for the test set
print(____.____(features_test,target_test)*100)
编辑并运行代码