[英]How to vectorize a list of words python?
我正在尝试将CountVectorizer模块与Sci-kit Learn一起使用。 从我读到的内容来看,它似乎可用于一系列句子,例如:
['这是第一个文件。','这是第二个第二个文件。','和第三个文件。','这是第一个文件吗?']
但是,有没有办法以列表形式矢量化一组单词,例如[['this','is','text','document','to','analyze'],['和', 'this','is','the','second'],['and','this','和','that','are','third']?
我试图使用' '.join(wordList)
将每个列表转换为句子,但我收到一个错误:
TypeError:序列项13329:期望的字符串或Unicode,找到生成器
当我试图跑:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.