Exercise

Inspecting your model

Now that you have built a "fake news" classifier, you'll investigate what it has learned. You can map the important vector weights back to actual words using some simple inspection techniques.

You have your well performing tfidf Naive Bayes classifier available as nb_classifier, and the vectors as tfidf_vectorizer.

Instructions

100 XP

Save the class labels as class_labels by accessing the .classes_ attribute of nb_classifier.

Extract the features using the .get_feature_names() method of tfidf_vectorizer.

Create a zipped array of the classifier coefficients with the feature names and sort them by the coefficients. To do this, first use zip() with the arguments nb_classifier.coef_[0] and feature_names. Then, use sorted() on this.

Print the top 20 weighted features for the first label of class_labels.

Print the bottom 20 weighted features for the second label of class_labels.