【发布时间】:2015-02-11 19:42:00
【问题描述】:
我正在自学如何使用 scikit-learn,我决定使用我自己的语料库开始 second task。我手动获得了一些二元组,比如说:
training_data = [[('this', 'is'), ('is', 'a'),('a', 'text'), 'POS'],
[('and', 'one'), ('one', 'more'), 'NEG']
[('and', 'other'), ('one', 'more'), 'NEU']]
我想以一种可以很好地填充 scikit-learn 提供的分类算法(svc、多项式朴素贝叶斯等)的格式对它们进行矢量化处理。这是我尝试过的:
from sklearn.feature_extraction.text import CountVectorizer
count_vect = CountVectorizer(analyzer='word')
X = count_vect.transform(((' '.join(x) for x in sample)
for sample in training_data))
print X.toarray()
问题在于我不知道如何处理标签(即'POS', 'NEG', 'NEU'),我是否也需要“矢量化”标签以便将training_data 传递给分类算法,或者我可以让它像'POS'或任何其他类型的字符串?另一个问题是我得到了这个:
raise ValueError("Vocabulary wasn't fitted or is empty!")
ValueError: Vocabulary wasn't fitted or is empty!
那么,我如何矢量化像 training_data 这样的二元组。我也在阅读有关dictvectorizer 和Sklearn-pandas 的信息,你们认为使用它们可能是完成这项任务的更好方法吗?
【问题讨论】:
标签: python machine-learning nlp scikit-learn nltk