我正在尝试将 CountVectorizer 模块与 Sci-kit Learn 一起使用。从我读到的来看,它似乎可以用在句子列表上,例如:
['这是第一个文档。','这是第二个文档。','第三个文档。', '这是第一个文档吗?']
但是,有没有一种方法可以向量化列表形式的单词集合,例如 [['this', 'is', 'text', 'document', 'to', 'analyze'], ['和”、“这个”、"is"、“这个”、“第二”]、[“和”、“这个”、“和”、“那个”、"is"、“第三”]?
我尝试使用 ' '.join(wordList)
将每个列表转换为句子,但出现错误:
TypeError: sequence item 13329: expected string or Unicode, generator found
当我尝试运行时:
vectorizer = CountVectorizer(min_df=50)
ratings = vectorizer.fit_transform([' '.join(wordList)])
谢谢!
最佳答案
我想你需要这样做:
counts = vectorizer.fit_transform(wordList) # sparse matrix with columns corresponding to words
words = vectorizer.get_feature_names() # array with words corresponding to columns
最后,得到[['this', 'is', 'text', 'document', 'to', 'analyze']]
sample_idx = 1
sample_words = [words[i] for i, count in
enumerate(counts.toarray()[sample_idx]) if count > 0]
关于python - 如何向量化Python单词列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42731011/