探索文本向量,第 2 部分
使用您在上一个练习中编写的 return_weights() 函数,提取文本向量中每个文档的最高权重词,返回词索引的列表,并用该列表将文本向量过滤到这些最高权重词。
本练习是课程的一部分
Python 中的机器学习预处理
练习说明
- 调用
return_weights(),返回该文档的最高权重词。 - 对返回的
filter_list调用set(),以去除重复的数字。 - 调用
words_to_filter,并传入以下参数:vocab作为vocab参数,tfidf_vec.vocabulary_作为original_vocab参数,text_tfidf作为vector参数,以及3以从每个文档中获取top_n为 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(____)]