我正在构建一个SGDClassifier,并使用tfidf转换器.除了从tfidf创建的功能外,我还想添加其他功能,如文档长度或其他评级.如何将这些功能添加到功能集中?以下是如何在管道中构造分类器:
data = fetch_20newsgroups(subset='train', categories=None)
pipeline = Pipeline([
('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', SGDClassifier()),
])
parameters = {
'vect__max_df': (0.5, 0.75, 1.0),
'vect__max_features': (None, 5000, 10000, 50000),
'vect__ngram_range': ((1, 1), (1, 2)), # unigrams or bigrams
'tfidf__use_idf': (True, False),
}
grid_search = gridsearchcv(pipeline, parameters, n_jobs=-1, verbose=1)
grid_search.fit(data.data, data.target)
print(grid_search.best_score_)
解决方法:
您可以使用功能联合http://scikit-learn.org/stable/modules/pipeline.html#featureunion-composite-feature-spaces
文档https://scikit-learn.org/0.18/auto_examples/hetero_feature_union.html中有一个很好的例子,我认为它完全符合您的要求.请参见TextStats转换器.
[更新:示例是针对scikit learn =< 0.18] 问候,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。