การจำกัดขนาดตัวอย่างขั้นต่ำ
อีกวิธีหนึ่งในการป้องกัน overfitting คือการกำหนดจำนวนการสังเกตขั้นต่ำที่จำเป็นสำหรับการสร้าง leaf (หรือ node) ใน Decision Tree
ในแบบฝึกหัดนี้ คุณจะ:
- กำหนดค่าขั้นต่ำนี้ที่ 100
- fit โมเดลใหม่กับข้อมูลพนักงาน
- ตรวจสอบผลการทำนายทั้งบนชุดข้อมูล training และ test
ตัวแปร features_train, target_train, features_test และ target_test พร้อมใช้งานใน workspace แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python
คำแนะนำการฝึกหัด
- สร้าง
DecisionTreeClassifierและกำหนดจำนวนการสังเกตขั้นต่ำของ leaf ที่ 100 - fit โมเดล Decision Tree กับข้อมูล training
- ตรวจสอบความแม่นยำของการทำนายบนทั้งชุดข้อมูล training และ test
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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)