使用特徵選擇訓練 Naive Bayes
現在你要重新執行在第 3 章結尾建立的 Naive Bayes 文字分類模型,但改用前一個練習中的特徵選擇結果:volunteer 資料集裡的 title 與 category_desc 欄位。
本練習屬於課程
Python 的 Machine Learning 前處理
練習說明
- 對文字向量
filtered_text與標籤y(也就是category_desc的標籤)使用train_test_split(),並將y傳入stratify參數,因為類別分布不均。 - 將
nbNaive Bayes 模型擬合到X_train與y_train。 - 計算
nb在測試集上的準確率。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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.____)