開發並測試最佳模型
在第 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 分析:用 Python 預測員工流失
練習說明
- 使用題目說明提供的參數初始化最佳模型。
- 僅使用訓練集中已選出的特徵來擬合模型。
- 以測試集中已選出的特徵進行預測。
- 列印模型的準確率(accuracy)、召回率(recall)以及 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)