How to input parameters in GridSearchCV for AdaBoostClassifier. ERROR: Invalid parameter learning_rate for estimatoR

How to input parameters in GridSearchCV for AdaBoostClassifier. ERROR: Invalid parameter learning_rate for estimatoR

我正在尝试使用 GridSearchCV 为我的模型调整参数。但是,我不断收到相同的错误,告诉我传递的参数网格包含无效参数。例如,它一直告诉我 invalid parameter learning_rate for estimator.....。这不仅发生在 Adaboost 上,而且发生在我尝试使用 GridsearchCV 调整的所有其他模型上,即逻辑回归、LinearSVC、决策树和 RandomForest。这是我为 Adaboost 分类器编写的代码以及我遇到的错误:

clf_adaboost  = Pipeline([('vect', CountVectorizer()),('tfidf', TfidfTransformer()),('clf', AdaBoostClassifier())
])
clf = Pipeline([
    ('vect', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', clf_adaboost)
])

parameters = {
    'n_estimators': [20, 50, 70, 100],
    'learning_rate' : [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3],
    'n_estimators' : [100, 200, 300, 400, 500]
    }

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=7)
gs_clf = GridSearchCV(clf, parameters, cv=kfold, n_jobs=-1)
gs_clf = gs_clf.fit(twenty_train.data, twenty_train.target)
print("Best score accurracy = %.3f%%" %((gs_clf.best_score_)*100.0))
print("Best parameters are : ")
print(gs_clf.best_params_)

当我 运行 执行此操作时,出现以下错误:

oblib.externals.loky.process_executor._RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/externals/loky/process_executor.py", line 418, in _process_worker
    r = call_item()
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/externals/loky/process_executor.py", line 272, in __call__
    return self.fn(*self.args, **self.kwargs)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/_parallel_backends.py", line 567, in __call__
    return self.func(*args, **kwargs)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/parallel.py", line 225, in __call__
    for func, args, kwargs in self.items]
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/parallel.py", line 225, in <listcomp>
    for func, args, kwargs in self.items]
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/model_selection/_validation.py", line 503, in _fit_and_score
    estimator.set_params(**parameters)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/pipeline.py", line 164, in set_params
    self._set_params('steps', **kwargs)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/utils/metaestimators.py", line 50, in _set_params
    super().set_params(**params)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/base.py", line 224, in set_params
    (key, self))
ValueError: Invalid parameter learning_rate for estimator Pipeline(memory=None,
         steps=[('vect',
                 CountVectorizer(analyzer='word', binary=False,
                                 decode_error='strict',
                                 dtype=<class 'numpy.int64'>, encoding='utf-8',
                                 input='content', lowercase=True, max_df=1.0,
                                 max_features=None, min_df=1,
                                 ngram_range=(1, 1), preprocessor=None,
                                 stop_words=None, strip_accents=None,
                                 token_pattern='(?u)\b\w\w+\b',
                                 tokenizer=None, vocabulary=Non...
                                                  preprocessor=None,
                                                  stop_words=None,
                                                  strip_accents=None,
                                                  token_pattern='(?u)\b\w\w+\b',
                                                  tokenizer=None,
                                                  vocabulary=None)),
                                 ('tfidf',
                                  TfidfTransformer(norm='l2', smooth_idf=True,
                                                   sublinear_tf=False,
                                                   use_idf=True)),
                                 ('clf',
                                  AdaBoostClassifier(algorithm='SAMME.R',
                                                     base_estimator=None,
                                                     learning_rate=1.0,
                                                     n_estimators=50,
                                                     random_state=None))],
                          verbose=False))],
         verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "twenty_news.py", line 61, in <module>
    gs_clf = gs_clf.fit(twenty_train.data, twenty_train.target)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/model_selection/_search.py", line 688, in fit
    self._run_search(evaluate_candidates)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/model_selection/_search.py", line 1149, in _run_search
    evaluate_candidates(ParameterGrid(self.param_grid))
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/sklearn/model_selection/_search.py", line 667, in evaluate_candidates
    cv.split(X, y, groups)))
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/parallel.py", line 934, in __call__
    self.retrieve()
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/parallel.py", line 833, in retrieve
    self._output.extend(job.get(timeout=self.timeout))
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/site-packages/joblib/_parallel_backends.py", line 521, in wrap_future_result
    return future.result(timeout=timeout)
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/concurrent/futures/_base.py", line 432, in result
    return self.__get_result()
  File "/Users/Furaha/.pyenv/versions/3.7.3/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
ValueError: Invalid parameter learning_rate for estimator Pipeline(memory=None,
         steps=[('vect',
                 CountVectorizer(analyzer='word', binary=False,
                                 decode_error='strict',
                                 dtype=<class 'numpy.int64'>, encoding='utf-8',
                                 input='content', lowercase=True, max_df=1.0,
                                 max_features=None, min_df=1,
                                 ngram_range=(1, 1), preprocessor=None,
                                 stop_words=None, strip_accents=None,
                                 token_pattern='(?u)\b\w\w+\b',
                                 tokenizer=None, vocabulary=Non...
                                                  preprocessor=None,
                                                  stop_words=None,
                                                  strip_accents=None,
                                                  token_pattern='(?u)\b\w\w+\b',
                                                  tokenizer=None,
                                                  vocabulary=None)),
                                 ('tfidf',
                                  TfidfTransformer(norm='l2', smooth_idf=True,
                                                   sublinear_tf=False,
                                                   use_idf=True)),
                                 ('clf',
                                  AdaBoostClassifier(algorithm='SAMME.R',
                                                     base_estimator=None,
                                                     learning_rate=1.0,
                                                     n_estimators=50,
                                                     random_state=None))],
                          verbose=False))],
         verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.

对于我尝试过的所有模型,我都遇到了类似的错误,我的猜测是我没有在参数网格中正确写入参数。有人可以帮我解决这个问题吗?

您正在加倍管道步骤。

你可以这样试试:

clf = Pipeline([
    ('vect', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', AdaBoostClassifier())
])

parameters = {
    'clf__n_estimators': [20, 50, 70, 100],
    'clf__learning_rate' : [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3]
    }

kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=7)
gs_clf = GridSearchCV(clf, parameters, cv=kfold, n_jobs=-1)
gs_clf = gs_clf.fit(twenty_train.data, twenty_train.target)
print("Best score accurracy = %.3f%%" %((gs_clf.best_score_)*100.0))
print("Best parameters are : ")
print(gs_clf.best_params_)

参考composite estimators and parameter spaces

在那种情况下,Gridsearch 将通过 adaboost 方法的网格。

你提到 n_estimators 两次。您是否也打算优化管道中的其他组件之一(但老实说我不这么认为,因为另一个组件的 none 有这个参数,只有 adaboost-ML-method 有这个超参数) ?