การคำนวณ Precision และ Recall
submodule sklearn.metrics มีฟังก์ชันมากมายที่ช่วยให้คำนวณ metrics ต่างๆ ได้อย่างสะดวก จนถึงตอนนี้ คุณได้คำนวณ precision และ recall ด้วยตนเองมาแล้ว ซึ่งเป็นสิ่งสำคัญสำหรับการสร้างความเข้าใจเชิงลึกเกี่ยวกับ metrics ทั้งสอง
ในทางปฏิบัติ เมื่อเข้าใจหลักการแล้ว สามารถใช้ฟังก์ชัน precision_score และ recall_score ที่คำนวณค่าเหล่านี้ให้อัตโนมัติได้เลย ทั้งสองฟังก์ชันทำงานคล้ายกับฟังก์ชันอื่นใน sklearn.metrics โดยรับ 2 อาร์กิวเมนต์ ได้แก่ label จริง (y_test) และ label ที่โมเดลทำนาย (y_pred)
ทีนี้มาลองใช้ขนาดชุดข้อมูลสำหรับ training ที่ 90% กัน
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Marketing Analytics: การพยากรณ์การเลิกใช้บริการของลูกค้าด้วย Python
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Import train_test_split
from sklearn.model_selection import train_test_split
# Create feature variable
X = telco.drop('Churn', axis=1)
# Create target variable
y = telco['Churn']
# Create training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
# Import RandomForestClassifier
from sklearn.ensemble import RandomForestClassifier
# Instantiate the classifier
clf = RandomForestClassifier()
# Fit to the training data
clf.fit(X_train, y_train)
# Predict the labels of the test set
y_pred = clf.predict(X_test)
# Import precision_score