始める無料で始める

高次 n-gram による感情分析

以前の演習と同様に、ある映画レビューがポジティブかネガティブかを判定する分類器を作成します。今回は、n=2 までの n-gram を使って取り組みます。

n-gram で処理した学習用レビューは X_train_ng にあります。対応するテスト用レビューは X_test_ng にあります。最後に、学習用とテスト用の感情クラスにはそれぞれ y_trainy_test を使用します。

この演習はコースの一部です

Pythonで学ぶNLPの特徴量エンジニアリング

コースを見る

演習の手順

  • MultinomialNB のインスタンスを作成し、clf_ng という名前を付けます。
  • X_train_ngy_train に対して分類器を学習させます(fit())。
  • score() メソッドを使って、X_test_ngy_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))
コードを編集して実行