Začněte nyníZačněte zdarma

N-gramy vyššího řádu pro analýzu sentimentu

Podobně jako v předchozím cvičení sestavíme klasifikátor, který dokáže rozpoznat, zda je recenze daného filmu pozitivní, nebo negativní. Tentokrát ale použijeme n-gramy až do n=2.

Trénovací recenze ve formátu n-gramů jsou dostupné jako X_train_ng, testovací recenze jako X_test_ng. Pro přístup k trénovacím a testovacím třídám sentimentu použij y_train a y_test.

Toto cvičení je součástí kurzu

Feature Engineering for NLP in Python

Zobrazit kurz

Pokyny k cvičení

  • Vytvoř instanci MultinomialNB a pojmenuj ji clf_ng.
  • Natrénuj klasifikátor na datech X_train_ng a y_train.
  • Změř accuracy na X_test_ng a y_test pomocí metody score().

Interaktivní cvičení na vyzkoušení si v praxi

Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.

# 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))
Upravit a spustit kód