始める無料で始める

特徴選択でNaive Bayesを学習する

ここでは、第3章の最後で実行したNaive Bayesによるテキスト分類モデルを、前の演習で選んだ特徴量(volunteer データセットの titlecategory_desc 列)で再実行します。

この演習はコースの一部です

Pythonで学ぶMachine Learningの前処理

コースを見る

演習の手順

  • filtered_text のテキストベクトルと y ラベル(category_desc のラベル)に対して train_test_split() を使い、クラス分布が不均衡なため、stratify パラメータには y を渡します。
  • 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.____)
コードを編集して実行