Scikits-learn:将自定义词汇与Pipeline一起使用

mat*_*ias 5 python machine-learning scikits scikit-learn

在我的scikits-learn Pipeline中,我想将自定义词汇表传递给CountVectorizer():

text_classifier = Pipeline([
    ('count', CountVectorizer(vocabulary=myvocab)),
    ('tfidf', TfidfTransformer()),
    ('clf', LinearSVC(C=1000))
])
Run Code Online (Sandbox Code Playgroud)

但是,据我所知,据我所知

text_classifier.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)

Pipeline使用CountVectorizer()的fit_transform()方法,它忽略了myvocab.我怎么能修改我的管道来使用myvocab?谢谢!

Fre*_*Foo 9

这是我五分钟前修复的scikit-learn中的一个错误.感谢您发现它.我建议您从Github升级到最新版本,或者将矢量化器与管道分开作为解决方法:

count = CountVectorizer(vocabulary=myvocab)
X_vectorized = count.transform(X_train)

text_classifier = Pipeline([
    ('tfidf', TfidfTransformer()),
    ('clf', LinearSVC(C=1000))
])

text_classifier.fit(X_vectorized, y_train)
Run Code Online (Sandbox Code Playgroud)

更新:自从这个答案发布以来,这个修补程序已被合并到几个scikit-learn版本中.