向 Sklearn 分类器添加特征
Add Features to An Sklearn Classifier
我正在构建一个 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]
此致,
我正在构建一个 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]
此致,