เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Voting Classifier

ลองมารวมโมเดล machine learning สามตัวเข้าด้วยกันเพื่อพัฒนาโมเดลตรวจจับการฉ้อโกงแบบ Random Forest ที่ผ่านมา โดยนำ Random Forest มารวมกับ Logistic Regression จากแบบฝึกหัดก่อนหน้า และ Decision Tree แบบเรียบง่าย สามารถใช้ get_model_results() เพื่อดูผลลัพธ์ของโมเดล ensemble ได้ทันที

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การตรวจจับการฉ้อโกงด้วย Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • นำเข้าแพ็กเกจ Voting Classifier
  • กำหนดโมเดลทั้งสามตัว ได้แก่ Logistic Regression จากแบบฝึกหัดก่อนหน้า, Random Forest จากแบบฝึกหัดที่ผ่านมา และ Decision Tree ที่มีการปรับ class weight ให้สมดุล
  • กำหนดโมเดล ensemble โดยใส่ตัวจำแนกทั้งสามตัวพร้อมป้ายกำกับของแต่ละตัว

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Import the package
from sklearn.ensemble import ____

# Define the three classifiers to use in the ensemble
clf1 = LogisticRegression(class_weight={0:1, 1:15}, random_state=5)
clf2 = ____(class_weight={0:1, 1:12}, criterion='gini', max_depth=8, max_features='log2',
            min_samples_leaf=10, n_estimators=30, n_jobs=-1, random_state=5)
clf3 = DecisionTreeClassifier(random_state=5, class_weight="____")

# Combine the classifiers in the ensemble model
ensemble_model = ____(estimators=[('lr', ____), ('rf', ____), ('dt', ____)], voting='hard')

# Get the results 
get_model_results(X_train, y_train, X_test, y_test, ensemble_model)
แก้ไขและรันโค้ด