Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Introduction to Natural Language Processing in Python

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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)
Code bewerken en uitvoeren