N-Gram อันดับสูงสำหรับการวิเคราะห์ความรู้สึก
คล้ายกับแบบฝึกหัดก่อนหน้า เราจะสร้างตัวจำแนกที่ตรวจสอบได้ว่ารีวิวภาพยนตร์นั้นเป็นเชิงบวกหรือเชิงลบ แต่คราวนี้จะใช้ n-gram ที่มีค่า n สูงสุดถึง 2 สำหรับงานนี้
ข้อมูลรีวิวสำหรับเทรนในรูปแบบ n-gram พร้อมใช้งานในตัวแปร X_train_ng และข้อมูลรีวิวสำหรับทดสอบพร้อมใช้งานใน X_test_ng ส่วน y_train และ y_test ใช้สำหรับเข้าถึงคลาสความรู้สึกของชุดเทรนและชุดทดสอบตามลำดับ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Feature Engineering for NLP in Python
คำแนะนำการฝึกหัด
- สร้าง instance ของ MultinomialNB และตั้งชื่อว่า
clf_ng - ฝึกตัวจำแนกด้วย
X_train_ngและy_train - วัดค่า
accuracyบนX_test_ngและy_testโดยใช้เมธอดscore()
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Define an instance of MultinomialNB
clf_ng = ____
# Fit the classifier
clf_ng.____(____, ____)
# Measure the accuracy
accuracy = ____
print("The accuracy of the classifier on the test set is %.3f" % accuracy)
# Predict the sentiment of a negative review
review = "The movie was not good. The plot had several holes and the acting lacked panache."
prediction = clf_ng.predict(ng_vectorizer.transform([review]))[0]
print("The sentiment predicted by the classifier is %i" % (prediction))