เปรียบเทียบประสิทธิภาพของโมเดล n-gram
ตอนนี้คุณรู้วิธีวิเคราะห์ความรู้สึก (sentiment analysis) โดยการแปลงข้อความเป็น n-gram รูปแบบต่าง ๆ แล้วส่งให้ตัวจำแนกประเภท (classifier) แล้ว ในแบบฝึกหัดนี้ เราจะวิเคราะห์ความรู้สึกของรีวิวภาพยนตร์ชุดเดิมโดยใช้โมเดล n-gram 2 แบบ ได้แก่ unigram และ n-gram ที่มี n สูงสุดถึง 3
จากนั้นเราจะเปรียบเทียบประสิทธิภาพโดยใช้เกณฑ์ 3 ข้อ ได้แก่ ความแม่นยำของโมเดลบนชุดทดสอบ เวลาที่ใช้ในการรันโปรแกรม และจำนวน feature ที่สร้างขึ้นจากการแทนค่าด้วย n-gram
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Feature Engineering for NLP in Python
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
start_time = time.time()
# Splitting the data into training and test sets
train_X, test_X, train_y, test_y = train_test_split(df['review'], df['sentiment'], test_size=0.5, random_state=42, stratify=df['sentiment'])
# Generating ngrams
vectorizer = ___
train_X = vectorizer.fit_transform(train_X)
test_X = vectorizer.transform(test_X)
# Fit classifier
clf = MultinomialNB()
clf.fit(train_X, train_y)
# Print accuracy, time and number of dimensions
print("The program took %.3f seconds to complete. The accuracy on the test set is %.2f. The ngram representation had %i features." % (time.time() - start_time, clf.score(test_X, test_y), train_X.shape[1]))