开始使用免费开始使用

探索文本向量,第 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(____)]
编辑并运行代码