LoslegenKostenlos loslegen

Training Naive Bayes with feature selection

You'll now re-run the Naive Bayes text classification model that you ran at the end of Chapter 3 with our selection choices from the previous exercise: the volunteer dataset's title and category_desc columns.

Diese Übung ist Teil des Kurses

Preprocessing for Machine Learning in Python

Kurs anzeigen

Anleitung zur Übung

  • Use train_test_split() on the filtered_text text vector, the y labels (which is the category_desc labels), and pass the y set to the stratify parameter, since we have an uneven class distribution.
  • Fit the nb Naive Bayes model to X_train and y_train.
  • Calculate the test set accuracy of nb.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Split the dataset according to the class distribution of category_desc
X_train, X_test, y_train, y_test = ____(____.toarray(), ____, stratify=____, random_state=42)

# Fit the model to the training data
nb.____

# Print out the model's accuracy
print(nb.____)
Code bearbeiten und ausführen