我想使用scikit-learn对包含列表的列表进行向量化。我会前往存放训练文本的路径,读取这些文本,然后得到类似这样的结果:
corpus = [["this is spam, 'SPAM'"],["this is ham, 'HAM'"],["this is nothing, 'NOTHING'"]]from sklearn.feature_extraction.text import CountVectorizervect = CountVectorizer(analyzer='word')vect_representation= vect.fit_transform(corpus)print vect_representation.toarray()
然后我得到了以下错误:
return lambda x: strip_accents(x.lower())AttributeError: 'list' object has no attribute 'lower'
此外,另一个问题是每个文档末尾的标签,我应该如何处理这些标签以进行正确的分类?
回答:
为了将来大家的参考,以下方法解决了我的问题:
corpus = [["this is spam, 'SPAM'"],["this is ham, 'HAM'"],["this is nothing, 'NOTHING'"]]from sklearn.feature_extraction.text import CountVectorizerbag_of_words = CountVectorizer(tokenizer=lambda doc: doc, lowercase=False).fit_transform(splited_labels_from_corpus)
当我使用.toarray()
函数时,输出如下:
[[0 0 1] [1 0 0] [0 1 0]]
谢谢大家