將決策樹套用到員工資料
進行訓練/測試切分後,你可以在訓練資料上開發分類器,並在其餘資料集上進行測試。在這個練習中,你會開始使用決策樹分類演算法,建立員工離職預測模型。此演算法提供 .fit() 方法,可用於在訓練集中將特徵與模型進行擬合。
提醒:目標與特徵都已經分成訓練與測試兩部分(Train:features_train、target_train;Test:features_test、target_test)。
本練習屬於課程
HR 分析:用 Python 預測員工流失
練習說明
- 匯入名為
DecisionTreeClassifier的分類演算法。 - 以
model作為變數名稱進行初始化,並將 random state 設為 42。 - 將訓練集的特徵擬合到
model,以套用決策樹模型。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the classification algorithm
from sklearn.tree import ____
# Initialize it and call model by specifying the random_state parameter
model = ____(random_state=42)
# Apply a decision tree model to fit features to the target
model.____(features_train, ____)