Get startedGet started for free

Training and testing the "fake news" model with TfidfVectorizer

Now that you have evaluated the model using the CountVectorizer, you'll do the same using the TfidfVectorizer with a Naive Bayes model.

The training and test sets have been created, and tfidf_vectorizer, tfidf_train, and tfidf_test have been computed. Additionally, MultinomialNB and metrics have been imported from, respectively, sklearn.naive_bayes and sklearn.

This exercise is part of the course

Introduction to Natural Language Processing in Python

View Course

Exercise instructions

  • Instantiate a MultinomialNB classifier called nb_classifier.
  • Fit the classifier to the training data.
  • Compute the predicted tags for the test data.
  • Calculate and print the accuracy score of the classifier.
  • Compute the confusion matrix. As in the previous exercise, specify the keyword argument labels=['FAKE', 'REAL'] so that the resulting confusion matrix is easier to read.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a Multinomial Naive Bayes classifier: nb_classifier
nb_classifier = ____

# Fit the classifier to the training data
____

# Create the predicted tags: pred
pred = ____

# Calculate the accuracy score: score
score = ____
print(score)

# Calculate the confusion matrix: cm
cm = ____
print(cm)
Edit and Run Code