用于情感分析的高阶 n-gram
与之前的练习类似,我们将构建一个分类器,用于判断某部电影的评论是正面还是负面。不过这一次,我们将使用最高到 n=2 的 n-gram 来完成任务。
n-gram 训练集评论可通过 X_train_ng 获取。相应的测试集评论可通过 X_test_ng 获取。最后,请使用 y_train 和 y_test 分别访问训练集和测试集的情感类别。
本练习是课程的一部分
Python 中的 NLP 特征工程
练习说明
- 定义一个 MultinomialNB 的实例,命名为
clf_ng。 - 在
X_train_ng和y_train上训练该分类器。 - 使用
score()方法在X_test_ng和y_test上评估accuracy。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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))