Ispezionare il tuo modello
Ora che hai costruito un classificatore di "fake news", andiamo a vedere cosa ha imparato. Puoi ricondurre i pesi più importanti del vettore alle parole reali usando alcune semplici tecniche di ispezione.
Hai a disposizione il tuo ottimo classificatore Naive Bayes con tf-idf come nb_classifier, e il vettorizzatore come tfidf_vectorizer.
Questo esercizio fa parte del corso
Introduzione al Natural Language Processing in Python
Istruzioni dell'esercizio
- Salva le etichette di classe come
class_labelsaccedendo all'attributo.classes_dinb_classifier. - Estrai le feature usando il metodo
.get_feature_names()ditfidf_vectorizer. - Crea un array zippato dei coefficienti del classificatore con i nomi delle feature e ordinali in base ai coefficienti. Per farlo, usa prima
zip()con gli argomentinb_classifier.coef_[0]efeature_names. Poi usasorted()su questo risultato. - Stampa le 20 feature con peso più alto per la prima etichetta di
class_labelse le 20 feature con peso più basso per la seconda etichetta diclass_labels. Questo è già stato fatto per te.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Get the class labels: class_labels
class_labels = ____
# Extract the features: feature_names
feature_names = ____
# Zip the feature names together with the coefficient array and sort by weights: feat_with_weights
feat_with_weights = ____(____(____, ____))
# Print the first class label and the top 20 feat_with_weights entries
print(class_labels[0], feat_with_weights[:20])
# Print the second class label and the bottom 20 feat_with_weights entries
print(class_labels[1], feat_with_weights[-20:])