【问题标题】:Issue with grid cross validation in decision tree regressor决策树回归器中的网格交叉验证问题
【发布时间】:2021-05-27 15:28:24
【问题描述】:

假设我已经定义了一个这样的回归器

tree = MultiOutputRegressor(DecisionTreeRegressor(random_state=0))
tree.fit(X_train, y_train)

现在我想做一个网格交叉验证来优化参数ccp_alpha(我不知道它是否是最好的优化参数,但我以它为例)。因此,我这样做:

alphas = np.arange(0,2,0.1)
pipe_tree = Pipeline(steps=[('scaler', scaler), ('pca', pca), ('tree', tree)], memory = 'tmp')
treeCV = GridSearchCV(pipe_tree, dict( pca__n_components=n_components, tree__ccp_alpha=alphas ), cv=5, scoring ='r2', n_jobs=-1)

start_time = time.time()
treeCV.fit(X_train, y_train)

问题是我拿这个问题:

ValueError: Invalid parameter ccp_alpha for estimator Pipeline(memory='tmp',
         steps=[('scaler', StandardScaler()), ('pca', PCA()),
                ('tree',
                 MultiOutputRegressor(estimator=DecisionTreeRegressor(random_state=0)))]). Check the list of available parameters with `estimator.get_params().keys()`.

如果我使用命令tree.get_params().keys(),它会打印一个可能的参数列表以在我的模型中更改。我认为问题在于GridSearchCV() 命令中的这个tree__ccp_alpha=alphas。但无论我做什么改变,它都不起作用。

【问题讨论】:

    标签: python python-3.x machine-learning scikit-learn regression


    【解决方案1】:

    我不确定您帖子中的 tree 是什么,但它似乎是您决策树顶部的多重回归量。如果您正确设置它应该可以工作。首先我们定义参数:

    from sklearn.preprocessing import StandardScaler
    from sklearn.decomposition import PCA
    from sklearn.pipeline import Pipeline
    from sklearn.model_selection import GridSearchCV
    from sklearn.tree import DecisionTreeClassifier
    import numpy as np
    
    alphas = np.arange(0,2,0.1)
    n_components = [3,4,5]
    

    然后设置步骤:

    scaler = StandardScaler()
    pca = PCA()
    from sklearn.multioutput import MultiOutputRegressor
    tree = MultiOutputRegressor(DecisionTreeClassifier())
    

    玩具数据:

    X_train = np.random.normal(0,1,(100,10))
    y_train = np.random.binomial(1,0.5,(100,2))
    

    管道:

    pipe_tree = Pipeline(steps=[('scaler', scaler), ('pca', pca), ('tree', tree)])
    tree.get_params()
    
    {'estimator__ccp_alpha': 0.0,
     'estimator__class_weight': None,
     'estimator__criterion': 'gini',
     'estimator__max_depth': None,
     'estimator__max_features': None,
     'estimator__max_leaf_nodes': None,
     'estimator__min_impurity_decrease': 0.0,
     'estimator__min_impurity_split': None,
     'estimator__min_samples_leaf': 1,
     'estimator__min_samples_split': 2,
     'estimator__min_weight_fraction_leaf': 0.0,
     'estimator__presort': 'deprecated',
     'estimator__random_state': None,
     'estimator__splitter': 'best',
     'estimator': DecisionTreeClassifier(),
     'n_jobs': None}
    

    参数应该是estimator__ccp_alpha。所以如果我们在它前面加上tree,加上tree__estimator__ccp_alpha = alphas,它就可以工作:

    treeCV = GridSearchCV(pipe_tree, dict( pca__n_components=n_components, tree__estimator__ccp_alpha=alphas ),
                          cv=5, scoring ='r2', n_jobs=-1)
    treeCV.fit(X_train, y_train)
    

    如果我使用你的:

    treeCV = GridSearchCV(pipe_tree, dict( pca__n_components=n_components, tree__ccp_alpha=alphas ),
                          cv=5, scoring ='r2', n_jobs=-1)
    

    我遇到同样的错误

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 1970-01-01
      • 2017-11-24
      • 2016-05-07
      • 1970-01-01
      • 2016-05-05
      • 2011-01-19
      • 2019-03-25
      • 2021-03-03
      相关资源
      最近更新 更多