テキストベクトルを探る(その2)
前の演習で作成した return_weights() 関数を使って、テキストベクトルの各ドキュメントから上位の単語を取り出し、単語のインデックスのリストを返し、そのリストを使ってテキストベクトルをそれらの上位単語のみに絞り込みます。
この演習はコースの一部です
Pythonで学ぶMachine Learningの前処理
演習の手順
- そのドキュメントで重みが高い単語を返すために、
return_weights()を呼び出してください。 - 返ってきた
filter_listに対してset()を呼び出し、重複した番号を取り除いてください。 words_to_filterを呼び出し、次のパラメータを渡してください:vocabパラメータにはvocab、original_vocabパラメータにはtfidf_vec.vocabulary_、vectorパラメータにはtext_tfidf、そして各ドキュメントから重み上位top_n3語を取得するために3を指定します。- 最後に、その
filtered_wordsセットをリストに変換し、テキストベクトルのフィルタとして使ってください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
def words_to_filter(vocab, original_vocab, vector, top_n):
filter_list = []
for i in range(0, vector.shape[0]):
# Call the return_weights function and extend filter_list
filtered = ____(vocab, original_vocab, vector, i, top_n)
filter_list.extend(filtered)
# Return the list in a set, so we don't get duplicate word indices
return ____(filter_list)
# Call the function to get the list of word indices
filtered_words = ____(____, ____, ____, ____)
# Filter the columns in text_tfidf to only those in filtered_words
filtered_text = text_tfidf[:, list(____)]