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

การประเมินผลสี่หมวดหมู่

Confusion matrix คือเครื่องมือที่ตรงไปตรงมาที่สุดในการดูผลลัพธ์ทั้งสี่หมวดหมู่ ได้แก่ true positives (TP), false positives (FP), true negatives (TN) และ false negatives (FN) ในแบบฝึกหัดนี้ จะใช้ decision tree classifier DecisionTreeClassifier() จาก sklearn กับข้อมูลคลิกตัวอย่าง แล้วคำนวณจำนวนผลลัพธ์แยกตามสี่หมวดหมู่

โมดูล pandas พร้อมใช้งานในชื่อ pd และ DataFrame ตัวอย่างถูกโหลดไว้เป็น df โดย features อยู่ใน X และ target อยู่ใน y นอกจากนี้ DecisionTreeClassifier จาก sklearn.tree ก็พร้อมใช้งานแล้ว

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

การพยากรณ์ CTR ด้วย Machine Learning ใน Python

ดูคอร์ส

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

  • แบ่งข้อมูล X และ y ออกเป็นชุด training และ testing
  • กำหนด decision tree classifier และสร้างการคาดการณ์ y_pred โดยการ fit โมเดล
  • ใช้ confusion matrix เพื่อนับจำนวนผลลัพธ์ในแต่ละหมวดหมู่ โดยให้ 1 แทนผลบวก (คลิก) และ 0 แทนผลลบ (ไม่คลิก)
  • ตัวอย่างเช่น: true negatives จะอยู่ที่ [0,0] และ true positives จะอยู่ที่ [1,1]

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

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

# Set up classifier using training data to predict test data
X_train, X_test, y_train, y_test = ____(
  X, y, test_size = .2, random_state = 0)
clf = ____
y_pred = clf.____(X_train, y_train).____(X_test) 

# Define confusion matrix and four categories
conf_matrix = ____(y_test, y_pred)
tn = conf_matrix[____][____]
fp = conf_matrix[____][____]
fn = conf_matrix[____][____]
tp = conf_matrix[____][____]

print("TN: %s, FP: %s, FN: %s, TP: %s" %(tn, fp, fn, tp))
แก้ไขและรันโค้ด