【问题标题】:Failing to tune decision tree classifier parameters using gridsearch无法使用网格搜索调整决策树分类器参数
【发布时间】:2019-05-29 18:39:56
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

  • 在您的param_grid 中使用max_depth 而不是decisiontreeclassifier__max_depth。 (同样的事情适用于其他参数。)您使用的符号是用于将多个估计器链接在一起的管道。

标签: python scikit-learn


【解决方案1】:

在您的param_grid 中使用max_depth 而不是decisiontreeclassifier__max_depth。 (同样的事情适用于其他参数。)您使用的符号是用于将多个估算器链接在一起的管道。

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)

【讨论】:

    猜你喜欢
    • 2020-07-28
    • 2021-05-16
    • 2017-01-14
    • 1970-01-01
    • 2021-05-27
    • 2019-10-15
    • 2018-10-20
    • 2018-12-12
    • 2021-06-14
    相关资源
    最近更新 更多