サンプルサイズを制限する
過学習を防ぐもう一つの方法は、Decision Tree で葉(またはノード)を成長させるために必要な最小観測数を指定することです。
この演習では、次を行います。
- この最小値を 100 に設定する
- 新しいモデルを従業員データに当てはめる
- 学習用データとテスト用データの両方で予測結果を確認する
変数 features_train、target_train、features_test、target_test はすでにワークスペースに用意されています。
この演習はコースの一部です
HRアナリティクス: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)