Get startedGet started for free

Training and testing the "fake news" model with CountVectorizer

Now it's your turn to train the "fake news" model using the features you identified and extracted. In this first exercise you'll train and test a Naive Bayes model using the CountVectorizer data.

The training and test sets have been created, and count_vectorizer, count_train, and count_test have been computed.

This exercise is part of the course

Introduction to Natural Language Processing in Python

View Course

Exercise instructions

  • Import the metrics module from sklearn and MultinomialNB from sklearn.naive_bayes.
  • 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. To make it easier to read, specify the keyword argument labels=['FAKE', 'REAL'].

Hands-on interactive exercise

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

# Import the necessary modules
____
____

# Instantiate 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