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

สร้างและประเมินโมเดล: ข้อมูลรีวิวสินค้า

ในแบบฝึกหัดนี้ จะได้สร้าง logistic regression โดยใช้ชุดข้อมูล reviews ซึ่งประกอบด้วยรีวิวสินค้า Amazon จากลูกค้า อาร์เรย์ y เก็บค่า sentiment ได้แก่ 1 สำหรับความรู้สึกเชิงบวก และ 0 สำหรับกรณีอื่น ส่วนอาร์เรย์ X เก็บฟีเจอร์ตัวเลขทั้งหมดที่สร้างด้วยแนวทาง BOW สามารถสำรวจข้อมูลเหล่านี้ใน IPython Shell ได้ตามต้องการ

โจทย์คือการสร้างโมเดล logistic regression และคำนวณค่า accuracy พร้อม confusion matrix โดยใช้ชุดข้อมูล test

ฟังก์ชัน logistic regression และการแบ่งข้อมูล train/test ถูกนำเข้าให้แล้ว

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

Sentiment Analysis ด้วย Python

ดูคอร์ส

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

  • นำเข้าฟังก์ชัน accuracy score และ confusion matrix
  • แบ่งข้อมูลเป็นชุด training และ testing โดยใช้ 30% เป็นชุด test และกำหนด random seed เป็น 42
  • เทรนโมเดล logistic regression
  • แสดงค่า accuracy score และ confusion matrix โดยใช้ข้อมูลชุด test

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

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

# Import the accuracy and confusion matrix
____

# Split the data into training and testing
X_train, X_test, y_train, y_test = ____(____, ____, ____=0.3, ____=42)

# Build a logistic regression
log_reg = ____._____

# Predict the labels 
y_predict = log_reg.predict(X_test)

# Print the performance metrics
print('Accuracy score of test data: ', ____(____, ____))
print('Confusion matrix of test data: \n', ____(____, ____)/len(y_test))
แก้ไขและรันโค้ด