0

我正在尝试使用 python mapper reducer 函数应用标记器。我有以下代码,但我不断收到错误。reducer 在列表中输出值,我将值传递给矢量化器。

from mrjob.job import MRJob
from sklearn.cross_validation import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer

class bagOfWords(MRJob):

def mapper(self, _, line):
    cat, phrase, phraseid, sentiment = line.split(',')
    yield (cat, phraseid, sentiment), phrase

def reducer(self, keys, values):

    yield keys, list(values)

#Output: ["Train", "--", "2"] ["A series of escapades demonstrating the adage that    what is good for the goose", "A series", "A", "series"]

def mapper(self, keys, values):
    vectorizer = CountVectorizer(min_df=0)
    vectorizer.fit(values)
    x = vectorizer.transform(values)
    x=x.toarray()       
    yield keys, (x)


if __name__ == '__main__':
    bagOfWords.run()

ValueError:空词汇;也许文档只包含停用词

感谢你们提供的任何帮助。

4

1 回答 1

0

CountVectorizer是有状态的:您需要在整个数据集上拟合相同的一个实例来构建词汇表,因此这不适合并行处理。

相反,您可以使用HashingVectorizerwhich is stateless (无需适合,您可以transform直接调用)。

于 2014-09-24T09:01:05.843 回答