Machine Learning › Tree และ boosting

ชุดฝึกท้าย module: Tree และ boosting

โจทย์คละหัวข้อทั้ง module โดยไม่บอกว่าต้องใช้เทคนิคไหน ให้เลือกวิธีเองเหมือนตอนทำงานจริง

🟢 พื้นฐานยังไม่ได้ทำ

ต้นไม้ที่ไม่จำกัดความลึก (DecisionTreeClassifier(random_state=42)) บน train มีกี่ใบ ลึกกี่ชั้น เก็บไว้ในตัวแปร n_leaves, depth

🟢 พื้นฐานยังไม่ได้ทำ

baseline เดาคลาสส่วนใหญ่ (DummyClassifier(strategy="most_frequent")) accuracy บนชุดทดสอบ ปัด 4 ตำแหน่ง เก็บไว้ในตัวแปร baseline_acc

🟢 พื้นฐานยังไม่ได้ทำ

random forest 100 ต้น (random_state=42): accuracy บนชุดทดสอบ ปัด 4 ตำแหน่ง เก็บไว้ในตัวแปร rf_test_acc

🟢 พื้นฐานยังไม่ได้ทำ

GradientBoostingClassifier(random_state=42): accuracy บนชุดทดสอบ ปัด 4 ตำแหน่ง เก็บไว้ในตัวแปร gb_test_acc

🟢 พื้นฐานยังไม่ได้ทำ

ต้นไม้ max_depth=2 (random_state=42) ใช้ feature ไหนถามเป็นข้อแรก (string) เก็บไว้ในตัวแปร root_feature

🟡 ประยุกต์ยังไม่ได้ทำ

CV 5-fold (skf) วัด ROC-AUC เฉลี่ยของต้นไม้ที่ max_depth = 1..8 (random_state=42) เก็บ depth ที่ AUC สูงสุดไว้ใน best_depth (เสมอเอาน้อยสุด) และค่า AUC (ปัด 4 ตำแหน่ง) ไว้ใน best_auc

🟡 ประยุกต์ยังไม่ได้ทำ

forest (n_estimators=100, min_samples_leaf=20, oob_score=True, random_state=42): OOB score และ accuracy บนชุดทดสอบ (ปัด 4 ตำแหน่ง) เก็บไว้ในตัวแปร oob, test_acc

🟡 ประยุกต์ยังไม่ได้ทำ

GB ที่เปิด early stopping (n_estimators=500, validation_fraction=0.1, n_iter_no_change=5, random_state=42): หยุดที่กี่รอบ และ accuracy บนชุดทดสอบ (ปัด 4 ตำแหน่ง) เก็บไว้ในตัวแปร stopped_at, es_acc

🟡 ประยุกต์ยังไม่ได้ทำ

ROC-AUC บนชุดทดสอบของ forest ที่มี 10 ต้น และ 200 ต้น (min_samples_leaf=10, random_state=42) ปัด 4 ตำแหน่ง เก็บไว้ในตัวแปร auc_10, auc_200

🟡 ประยุกต์ยังไม่ได้ทำ

GridSearchCV (cv=skf, scoring="roc_auc") บน GradientBoostingClassifier(random_state=42) ด้วย grid learning_rate ∈ [0.05, 0.1] × n_estimators ∈ [20, 50] เก็บค่า learning_rate และ n_estimators ที่ดีที่สุดไว้ใน gb_best_lr, gb_best_n

🟡 ประยุกต์ยังไม่ได้ทำ

CV 5-fold (skf บน X, y ทั้งชุด) ROC-AUC เฉลี่ยของ LogisticRegression(max_iter=2000) และ GradientBoostingClassifier(random_state=42) ปัด 4 ตำแหน่ง เก็บไว้ในตัวแปร logreg_auc, gb_auc

🔴 ท้าทายยังไม่ได้ทำ

GB ค่าเริ่มต้น (random_state=42): permutation importance บนชุดทดสอบ (n_repeats=5, random_state=42, scoring="roc_auc") feature ไหนสำคัญสุด (string) เก็บไว้ใน top_feature และค่า (ปัด 3 ตำแหน่ง) ไว้ใน top_value

🔴 ท้าทายยังไม่ได้ทำ

เฉลี่ย predict_proba (คลาส 1) ของต้นไม้ depth 3, forest (100 ต้น, min_samples_leaf=10) และ GB ค่าเริ่มต้น (ทุกตัว random_state=42) แล้ววัด ROC-AUC บนชุดทดสอบ เก็บไว้ใน blend_auc และ AUC ที่ดีที่สุดในสามโมเดลเดี่ยวไว้ใน best_single_auc (ปัด 4 ตำแหน่ง)

🔴 ท้าทายยังไม่ได้ทำ

ผลของขนาดข้อมูลเทรน: fit forest (100 ต้น, min_samples_leaf=10, random_state=42) บน 500 แถวแรกของ train, 1,000 แถวแรก และทั้งหมด แล้ววัด accuracy บนชุดทดสอบ (ปัด 4 ตำแหน่ง) เก็บไว้ในตัวแปร acc_500, acc_1000, acc_all

🔴 ท้าทายยังไม่ได้ทำ

ต้นไม้ max_depth=3 (random_state=42): จัดกลุ่มแถวของชุดทดสอบตามใบที่ตก (tree.apply) — ใบที่มีอัตราซื้อซ้ำจริงสูงสุด มีอัตราเท่าไร (ปัด 4 ตำแหน่ง) เก็บไว้ใน top_leaf_rate และมีกี่แถวในใบนั้นเก็บไว้ใน top_leaf_n

🔴 ท้าทายยังไม่ได้ทำ

ต้นไม้ก็ทำ regression ได้: ทำนาย first_order_value (X เดิม, split 75/25 random_state=42 ไม่ stratify) ด้วย RandomForestRegressor(n_estimators=100, min_samples_leaf=5, random_state=42) และ LinearRegression — R² บนชุดทดสอบ (ปัด 4 ตำแหน่ง) เก็บไว้ใน rf_r2, lin_r2