开发并测试最佳模型
在第 3 章中,您发现以下参数可以得到更好的模型:
max_depth = 8,min_samples_leaf = 150,class_weight = "balanced"
在本章中,您又发现部分特征影响可以忽略。您意识到只用少量精心挑选且影响力大的特征也能获得准确预测,并据此更新了训练集和测试集,创建了变量 features_train_selected 和 features_test_selected。
基于这些信息,您现在将开发用于预测员工流失的最佳模型,并用合适的评估指标进行评估。
features_train_selected 和 features_test_selected 变量已在您的工作区中可用,函数 recall_score 和 roc_auc_score 也已为您导入。
本练习是课程的一部分
HR Analytics:用 Python 预测员工流失
练习说明
- 使用描述中提供的参数初始化最佳模型。
- 仅用训练集中筛选后的特征来拟合模型。
- 基于测试集中筛选后的特征进行预测。
- 打印模型的准确率、召回率和 ROC/AUC 分数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Initialize the best model using parameters provided in description
model_best = DecisionTreeClassifier(____=____, ____=____, ____=____, random_state=42)
# Fit the model using only selected features from training set: done
model_best.fit(____, target_train)
# Make prediction based on selected list of features from test set
prediction_best = model_best.____(____)
# Print the general accuracy of the model_best
print(____.score(features_test_selected, target_test) * 100)
# Print the recall score of the model predictions
print(____(target_test, prediction_best) * 100)
# Print the ROC/AUC score of the model predictions
print(roc_auc_score(target_test, ____) * 100)