简体   繁体   English

Scikit-learn多输出分类器使用:GridSearchCV,Pipeline,OneVsRestClassifier,SGDClassifier

[英]Scikit-learn multi-output classifier using: GridSearchCV, Pipeline, OneVsRestClassifier, SGDClassifier

I am attempting to build a multi-output model with GridSearchCV and Pipeline. 我正在尝试使用GridSearchCV和Pipeline构建一个多输出模型。 The Pipeline is giving me trouble because standard classifier examples don't have the OneVsRestClassifier() wrapping the classifier. 管道给我带来麻烦,因为标准分类器示例没有包装分类器的OneVsRestClassifier()。 I'm using scikit-learn 0.18 and python 3.5 我正在使用scikit-learn 0.18和python 3.5

## Pipeline: Train and Predict
## SGD: support vector machine (SVM) with gradient descent
from sklearn.multiclass import OneVsRestClassifier
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier

clf = Pipeline([
               ('vect', CountVectorizer(ngram_range=(1,3), max_df=0.50 ) ),
               ('tfidf', TfidfTransformer() ),
               ('clf', SGDClassifier(loss='modified_huber', penalty='elasticnet',
                                          alpha=1e-4, n_iter=5, random_state=42,
                                          shuffle=True, n_jobs=-1) ),
                ])

ovr_clf = OneVsRestClassifier(clf ) 

from sklearn.model_selection import GridSearchCV
parameters = {'vect__ngram_range': [(1,1), (1,3)],
              'tfidf__norm': ('l1', 'l2', None),
              'estimator__loss': ('modified_huber', 'hinge',),
             }

gs_clf = GridSearchCV(estimator=pipeline, param_grid=parameters, 
                      scoring='f1_weighted', n_jobs=-1, verbose=1)
gs_clf = gs_clf.fit(X_train, y_train)

But this yields the error: .... 但这会产生错误:....

ValueError: Invalid parameter estimator for estimator Pipeline(steps=[('vect', CountVectorizer(analyzer='word', binary=False, decode_error='strict', dtype=, encoding='utf-8', input='content', lowercase=True, max_df=0.5, max_features=None, min_df=1, ngram_range=(1, 3), preprocessor=None, stop_words=None, strip...er_t=0.5, random_state=42, shuffle=True, verbose=0, warm_start=False), n_jobs=-1))]). ValueError:估算器管道的无效参数估计器(steps = [('vect',CountVectorizer(analyzer ='word',binary = False,decode_error ='strict',dtype =,encoding ='utf-8',input ='content ',lowercase = True,max_df = 0.5,max_features = None,min_df = 1,ngram_range =(1,3),预处理器= None,stop_words = None,strip ... er_t = 0.5,random_state = 42,shuffle = True, verbose = 0,warm_start = False),n_jobs = -1))])。 Check the list of available parameters with estimator.get_params().keys() . 使用estimator.get_params().keys()检查可用参数列表。

So what is the correct way to pass parameters to clf through the OneVsRestClassifier using param_grid and Pipeline? 那么使用param_grid和Pipeline通过OneVsRestClassifier将参数传递给clf的正确方法是什么? Do I need to separate the vectorizer and tdidf from the classifier in the Pipeline? 我是否需要将矢量化器和tdidf与管道中的分类器分开?

Pass OneVsRestClassifier() as a step of pipeline itself and SGDClassifier as estimator of OneVsRestClassifier. 将OneVsRestClassifier()作为管道本身的一步,并将SGDClassifier作为OneVsRestClassifier的估算器。 You can go like this. 你可以这样。

pipeline = Pipeline([
               ('vect', CountVectorizer(ngram_range=(1,3), max_df=0.50 ) ),
               ('tfidf', TfidfTransformer() ),
               ('clf', OneVsRestClassifier(SGDClassifier(loss='modified_huber', penalty='elasticnet',
                                          alpha=1e-4, n_iter=5, random_state=42,
                                          shuffle=True, n_jobs=-1) )),
                ])

Rest of the code can remain same. 其余代码可以保持不变。 OneVsRestClassifier acts as a wrapper on other estimators. OneVsRestClassifier充当其他估算器的包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM