tf/idf ベクトルを用いたテキスト分類
volunteer データセットの title 列を tf/idf ベクトルにエンコードできたので、これらのベクトルを使って category_desc 列を予測します。
この演習はコースの一部です
Pythonで学ぶMachine Learningの前処理
演習の手順
- クラス分布が不均衡なので、
text_tfidfベクトルと目的変数yを訓練用とテスト用に分割し、stratifyパラメータをyに設定します。scikit-learn に適した形式にするため、tf/idf ベクトルに対して.toarray()メソッドを実行する必要がある点にご注意ください。 - Naive Bayes モデル
nbにX_trainとy_trainを学習させます。 - テストデータの正解率を出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Split the dataset according to the class distribution of category_desc
y = volunteer["category_desc"]
X_train, X_test, y_train, y_test = ____(____.toarray(), ____, ____=____, random_state=42)
# Fit the model to the training data
nb.____(____, ____)
# Print out the model's accuracy
print(nb.____(____, ____))