使用Python scikit-learn的网格搜索方法时出现无效参数错误

我正在学习如何使用scikit-learn中的GridSearchCV()方法来寻找决策树分类器的最优超参数。

问题是,如果我只指定一个参数的选项,一切正常,如下所示:

print(__doc__)# Code source: Gael Varoquaux# Modified for documentation by Jaques Grobler# License: BSD 3 clausefrom sklearn import datasetsfrom sklearn.grid_search import GridSearchCVfrom sklearn.tree import DecisionTreeClassifier# define classifierdt = DecisionTreeClassifier()# import some data to play withiris = datasets.load_iris()X = iris.data[:, :2]  # we only take the first two features.y = iris.target# define parameter values that should be searchedmin_samples_split_options = range(2, 4)# create a parameter grid: map the parameter names to the values that should be savedparam_grid_dt = dict(min_samples_split= min_samples_split_options) # for DT# instantiate the gridgrid = GridSearchCV(dt, param_grid_dt, cv=10, scoring='accuracy')# fit the grid with paramgrid.fit(X, y)# view complete resultsgrid.grid_scores_'''# examine results from first tupleprint grid.grid_scores_[0].parametersprint grid.grid_scores_[0].cv_validation_scoresprint grid.grid_scores_[0].mean_validation_score'''# examine the best modelprint '*******Final results*********'print grid.best_score_print grid.best_params_print grid.best_estimator_

结果:

None*******Final results*********0.68{'min_samples_split': 3}DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,            max_features=None, max_leaf_nodes=None, min_samples_leaf=1,            min_samples_split=3, min_weight_fraction_leaf=0.0,            presort=False, random_state=None, splitter='best')

但是,当我添加另一个参数的选项时,会出现“无效参数”错误,如下所示:

print(__doc__)# Code source: Gael Varoquaux# Modified for documentation by Jaques Grobler# License: BSD 3 clausefrom sklearn import datasetsfrom sklearn.grid_search import GridSearchCVfrom sklearn.tree import DecisionTreeClassifier# define classifierdt = DecisionTreeClassifier()# import some data to play withiris = datasets.load_iris()X = iris.data[:, :2]  # we only take the first two features.y = iris.target# define parameter values that should be searchedmax_depth_options = range(10, 251) # for DTmin_samples_split_options = range(2, 4)# create a parameter grid: map the parameter names to the values that should be savedparam_grid_dt = dict(max_depth=max_depth_options, min_sample_split=min_samples_split_options) # for DT# instantiate the gridgrid = GridSearchCV(dt, param_grid_dt, cv=10, scoring='accuracy')# fit the grid with paramgrid.fit(X, y)'''# view complete resultsgrid.grid_scores_# examine results from first tupleprint grid.grid_scores_[0].parametersprint grid.grid_scores_[0].cv_validation_scoresprint grid.grid_scores_[0].mean_validation_score# examine the best modelprint '*******Final results*********'print grid.best_score_print grid.best_params_print grid.best_estimator_'''

结果:

NoneTraceback (most recent call last):  File "C:\Users\KubiK\Desktop\GridSearch_ex6.py", line 31, in <module>    grid.fit(X, y)  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\grid_search.py", line 804, in fit    return self._fit(X, y, ParameterGrid(self.param_grid))  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\grid_search.py", line 553, in _fit    for parameters in parameter_iterable  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 800, in __call__    while self.dispatch_one_batch(iterator):  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 658, in dispatch_one_batch    self._dispatch(tasks)  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 566, in _dispatch    job = ImmediateComputeBatch(batch)  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 180, in __init__    self.results = batch()  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\externals\joblib\parallel.py", line 72, in __call__    return [func(*args, **kwargs) for func, args, kwargs in self.items]  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\cross_validation.py", line 1520, in _fit_and_score    estimator.set_params(**parameters)  File "C:\Users\KubiK\Anaconda2\lib\site-packages\sklearn\base.py", line 270, in set_params    (key, self.__class__.__name__))ValueError: Invalid parameter min_sample_split for estimator DecisionTreeClassifier. Check the list of available parameters with `estimator.get_params().keys()`.[Finished in 0.3s]

回答:

你的代码中有一个拼写错误,应该是min_samples_split而不是min_sample_split

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注