開始使用免費開始

限制樣本大小

在決策樹中,另一種防止過度擬合的方法是指定成長一個樹葉(或節點)所需的最少觀測數。

在這個練習中,你將:

  • 將此最小門檻設為 100
  • 將新模型套用到員工資料
  • 檢視在訓練集與測試集上的預測結果

變數 features_traintarget_trainfeatures_testtarget_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)
編輯並執行程式碼