Performance metric ของข้อมูล Twitter
ในแบบฝึกหัดนี้ คุณจะฝึก logistic regression model เพื่อพยากรณ์ sentiment ของทวีต และประเมินประสิทธิภาพบนชุดข้อมูล test ด้วย metric หลายรูปแบบ
มีการสร้าง matrix X ไว้ให้แล้ว ซึ่งประกอบด้วย feature ที่สร้างจาก BOW บนคอลัมน์ text
Label ถูกเก็บไว้ใน vector ชื่อ y โดย y มีค่าเป็น 0 สำหรับทวีตเชิงลบ 1 สำหรับทวีตกลาง ๆ และ 2 สำหรับทวีตเชิงบวก
แม้จะมี 3 class แต่นี่ยังคงเป็นปัญหาการจำแนกประเภท (classification) ค่า accuracy ยังคงวัดสัดส่วนของ instance ที่พยากรณ์ได้ถูกต้อง ส่วน confusion matrix จะมีขนาด 3x3 แต่ละแถวแสดงจำนวน case ที่พยากรณ์ได้สำหรับ class 2, 1 และ 0 และแต่ละคอลัมน์แสดงจำนวน case จริงใน class 2, 1 และ 0
ได้ import package ที่จำเป็นทั้งหมดไว้ให้แล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Sentiment Analysis ด้วย Python
คำแนะนำการฝึกหัด
- แบ่งข้อมูลเป็นชุด train/test และใช้ stratify ตาม
y - ฝึก logistic regression classifier
- พยากรณ์ผลลัพธ์บนชุดข้อมูล test
- แสดงค่า accuracy score และ confusion matrix ที่ได้จากชุดข้อมูล test
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = ____(X, y, test_size=0.3, random_state=123, ____=y)
# Train a logistic regression
log_reg = ____.____(___, ____)
# Make predictions on the test set
y_predicted = log_reg.____(___)
# Print the performance metrics
print('Accuracy score test set: ', ____(y_test, y_predicted))
print('Confusion matrix test set: \n', ____(y_test, y_predicted)/len(y_test))