我試圖利用scikit-learn的make_pipeline()以及GridSearchCV()。 這個管道很簡單,只包括兩個步驟,一個StandardScaler()和一個MLPRegressor()。GridSearchCV()也很簡單,但我使用了TimeSeriesSplit()來進行交叉驗證。
我得到的錯誤如下:
ValueError。估算器的引數MLPRegressor無效 Pipeline(step=[('standardcaler', StandardScaler()), ('mlpregressor', MLPRegressor()])。用 estimator.get_params().keys()檢查可用的引數串列。
誰能幫助我們了解一下可用的引數串列?
誰能幫助我理解如何糾正這個問題,以便我可以使用make_pipeline()框架,同時使用GridSearchCV()和MLPRegressor()。
from sklearn.neural_network import MLPRegressor
...: from sklearn.preprocessing import StandardScaler
...: from sklearn.model_selection import TimeSeriesSplit, GridSearchCV
...: from sklearn.pipeline import make_pipeline
...: import numpy as np
在 [2]: tscv = TimeSeriesSplit(n_splits = 5)
在 [3]: pipe = make_pipeline(StandardScaler(), MLPRegressor() )
在 [4]: param_grid = {'MLPRegressor__hidden_layer_sizes': [(16,16,), (64,64, ), (
...: 128,128,)], 'MLPRegressor__activation'/span>。['identity'/span>, 'logistic'/span>, 'tanh'/span>,
...: 'relu'],'MLPRegressor__solver': ['adam','sgd']}。
在 [5]: grid = GridSearchCV(pipe, param_grid = param_grid, cv = tscv)
在[6]: features = np.random.random([1000,10] )
在[7]: target = np.random.normal(0,10,1000)
在[8]: grid.fit(features, target)
---------------------------------------------------------------------------
ValueError 回溯(最近的一次呼叫)。
<ipython-input-8-7233f9f2005e> in < module>
----> 1 grid.fit(features, target)
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
61 extra_args = len(args) - len(all_args)
62 if extra_args <= 0:
---> 63 return f(*args, **kwargs)
64
65 # extra_args > 0
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)。
839 return result
840 return結果
--> 841 self._run_search(evaluated_candidates)
842
843 # multimetric在這里被確定,因為在可呼叫的情況下
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/model_selection/_search.py in _run_search(self, evaluate_candidates)
1294 def _run_search(self, evaluate_candidates)。
1295 ""Search all candidates in param_grid""
-> 1296 evaluate_candidates(ParameterGrid(self.param_grid) )
1297
1298
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/model_selection/_search.py in evaluate_candidates( candidate_params, cv, more_results)
793 n_splits, n_candidates, n_candidates* n_splits))
794
--> 795 out = parallel(delayed(_fit_and_score)(clone(base_estimator),
796 X, y,
797 train=train, test=test,
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/parallel.py in __call__(self, iterable)。
1039 #剩余作業。
1040 self._iterating = False.
-> 1041 if self.dispatch_one_batch(iterator)。
1042 self._iterating = self._original_iterator is not None
1043
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)。
857 return False
858 else:
--> 859 self._dispatch(tasks)
860 return True
861
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/parallel.py in _dispatch(self, batch)。
775 with self._lock:
776 job_idx = len(self._jobs)
--> 777 job = self._backend.apply_async(batch, callback=cb)
778 # 一個作業可以比它的回呼快完成。
779 # 在我們到達這里之前被呼叫,導致self._jobs
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/_parallel_backends.py in apply_async(self, func, callback)。
206 def apply_async(self, func, callback=None) 。
207 ""Schedule a func to be run"".
--> 208 result = ImmediateResult(func)
209 if callback:
210 callback(result)
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/_parallel_backends.py in __init__(self, batch)。
570 # 不要延遲應用,以避免保留輸入。
571 # argument in memory
--> 572 self.results = batch()
573
574 def get(self)。
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/parallel.py in __call__(self)
260 # 更改默認的行程數為-1。
261 with parallel_backend(self._backend, n_jobs=self._n_jobs):
--> 262 return [func(*args, **kwargs)
263 for func, args, kwargs in self.items ]
264
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/joblib/parallel.py in <listcomp>(.0)
260 # 更改默認的行程數為-1。
261 with parallel_backend(self._backend, n_jobs=self._n_jobs):
--> 262 return [func(*args, **kwargs)
263 for func, args, kwargs in self.items ]
264
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/utils/fixes.py in __call__(self, *args, **kwargs)
220 def __call__(self, *args, **kwargs)。
221 with config_context(**self.config)。
--> 222 return self.function(*args, **kwargs)
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/model_selection/_validation. py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, split_progress, candidate_progress, error_score)
584 cloned_parameters[k] = clone(v, safe=False)
585。
--> 586 estimator = estimator.set_params(**cloned_parameters)
587
588 start_time = time.time()
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/pipeline.py in set_params(self, **kwargs)
148 self
149 ""
--> 150 self._set_params('steps', **kwargs)
151 回傳自我
152
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/utils/metaestimators.py in _set_params(self, attr, **params)
52 self._replace_estimator(attr, name, params.pop(name))
53 # 3. 步驟引數和其他初始化引數
--> 54 super().set_params(**params)
55 回傳自我
56
~/opt/miniconda3/envs/practice/lib/python3.9/site-packages/sklearn/base.py in set_params(self, **params)
228 key, delim, sub_key = key.partition('__')
229 如果key不在valid_params中。
--> 230 raise ValueError('Invalid parameter %s for estimator %s. '
231 '檢查可用引數的串列'
232 '用`estimator.get_params().keys()`。%
ValueError: 估算器的引數MLPRegressor無效 Pipeline(step=[('standardcaler', StandardScaler()),
('mlpregressor', MLPRegressor()])。用`estimator.get_params().keys()`檢查可用引數的串列。
uj5u.com熱心網友回復:
解決方法
。是的。首先制作管道。然后將管道作為你的模型,并將其傳遞給 GridSearchCV。
你的問題出在下面一行(你把它標錯了):
將MLPRegressor__ 替換為 mlpregressor__。
修復:
- 用于
MLPRegressor估計器的管道named_step在param_grid中被誤標為MLPRegressor__。- 將其改為
mlpregressor__后,問題得以解決。
你可以在這個colab筆記本中運行和檢查它。
# INCORRECT
param_grid = {
'MLPRegressor__hidden_layer_sizes': [(16, 16,), (64, 64,), (128, 128,)] 。
'MLPRegressor__activation': ['ident', 'logistic', 'tanh', 'relu']。
'MLPRegressor__solver'/span>: ['adam', 'sgd'],
}
# CORRECTED
param_grid = {
'mlpregressor__hidden_layer_sizes': [(16, 16,), (64, 64,), (128, 128,)] 。
'mlpregressor__activation': ['identity', 'logistic', 'tanh', 'relu'],
'mlpregressor__solver'/span>: ['adam'/span>, 'sgd'/span>],
}
注意事項
了解這里的問題的關鍵是觀察錯誤堆疊的最后兩行。ValueError: 無效引數MLPRegressor for estimator Pipeline(steps=[('standardcaler', StandardScaler())。
('mlpregressor', MLPRegressor()])。用`estimator.get_params().keys()`檢查可用引數的串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321121.html
標籤:
上一篇:【C語言】??自定義型別(結構體+位段+列舉+聯合體)【建議收藏】
下一篇:資料庫優化之SQL陳述句優化技巧
