開始使用免費開始

使用特徵選擇訓練 Naive Bayes

現在你要重新執行在第 3 章結尾建立的 Naive Bayes 文字分類模型,但改用前一個練習中的特徵選擇結果:volunteer 資料集裡的 titlecategory_desc 欄位。

本練習屬於課程

Python 的 Machine Learning 前處理

檢視課程

練習說明

  • 對文字向量 filtered_text 與標籤 y(也就是 category_desc 的標籤)使用 train_test_split(),並將 y 傳入 stratify 參數,因為類別分布不均。
  • nb Naive Bayes 模型擬合到 X_trainy_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.____)
編輯並執行程式碼