无法使用gridsearch调整决策树分类器参数

时间:2019-05-29 18:39:56

标签: python scikit-learn

我正在尝试使用GridSearchCV调整参数,但一直遇到此错误消息

ValueError: Invalid parameter decisiontreeclassifier for estimator DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, presort=False, random_state=None,
            splitter='best'). Check the list of available parameters with `estimator.get_params().keys()`.

这是我编写的代码

accuracy_score = make_scorer(accuracy_score,greater_is_better = True)
dtc = DecisionTreeClassifier()
depth = np.arange(1,30)
leaves = [1,2,4,5,10,20,30,40,80,100]
param_grid =[{'decisiontreeclassifier__max_depth':depth,
             'decisiontreeclassifier__min_samples_leaf':leaves}]
grid_search = GridSearchCV(estimator = dtc,param_grid = param_grid,
                           scoring=accuracy_score,cv=10)
grid_search = grid_search.fit(X_train,y_train)

1 个答案:

答案 0 :(得分:0)

在您的max_depth中使用decisiontreeclassifier__max_depth代替param_grid。 (同样的事情也适用于其他参数。)您使用的表示法是用于将多个估算器链接在一起的管道。

accuracy_score = make_scorer(accuracy_score,greater_is_better = True)
dtc = DecisionTreeClassifier()
depth = np.arange(1,30)
leaves = [1,2,4,5,10,20,30,40,80,100]
param_grid =[{'max_depth':depth,
             'min_samples_leaf':leaves}]
grid_search = GridSearchCV(estimator = dtc,param_grid = param_grid,
                           scoring=accuracy_score,cv=10)
grid_search = grid_search.fit(X_train,y_train)