GridSearchCV库参数值必须是序列号

2024-05-23 16:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码片段:

# TODO: Import 'make_scorer', 'DecisionTreeRegressor', and 'GridSearchCV'
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import make_scorer
from sklearn.grid_search import GridSearchCV

def fit_model(X, y):
    """ Performs grid search over the 'max_depth' parameter for a 
        decision tree regressor trained on the input data [X, y]. """

    # Create cross-validation sets from the training data
    # sklearn version 0.18: ShuffleSplit(n_splits=10, test_size=0.1, train_size=None, random_state=None)
    # sklearn versiin 0.17: ShuffleSplit(n, n_iter=10, test_size=0.1, train_size=None, random_state=None)
    cv_sets = ShuffleSplit(X.shape[0], n_iter = 10, test_size = 0.20, random_state = 0)

    # TODO: Create a decision tree regressor object
    regressor = DecisionTreeRegressor()

    # TODO: Create a dictionary for the parameter 'max_depth' with a range from 1 to 10
    params = {'max_depth' : range(1, 10)}

    # TODO: Transform 'performance_metric' into a scoring function using 'make_scorer' 
    scoring_fnc = make_scorer(performance_metric)

    # TODO: Create the grid search cv object --> GridSearchCV()
    # Make sure to include the right parameters in the object:
    # (estimator, param_grid, scoring, cv) which have values 'regressor', 'params', 'scoring_fnc', and 'cv_sets' respectively.
    scoring = scoring_fnc
    cv = cv_sets
    grid = GridSearchCV(regressor, params, scoring, cv)

    # Fit the grid search object to the data to compute the optimal model
    grid = grid.fit(X, y)

    # Return the optimal model after fitting the data
    return grid.best_estimator_

当我调用以下几行时,我得到一个错误:

^{pr2}$

错误是:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-48-ede909fc46d6> in <module>()
      1 # Fit the training data to the model using grid search
----> 2 reg = fit_model(X_train, y_train)
      3 
      4 # Produce the value for 'max_depth'
      5 print("Parameter 'max_depth' is {} for the optimal model.".format(reg.get_params()['max_depth']))

<ipython-input-47-8bc5d7617f09> in fit_model(X, y)
     27     scoring = scoring_fnc
     28     cv = cv_sets
---> 29     grid = GridSearchCV(regressor, params, scoring, cv)
     30 
     31     # Fit the grid search object to the data to compute the optimal model

/opt/conda/lib/python3.6/site-packages/sklearn/grid_search.py in __init__(self, estimator, param_grid, scoring, fit_params, n_jobs, iid, refit, cv, verbose, pre_dispatch, error_score)
    819             refit, cv, verbose, pre_dispatch, error_score)
    820         self.param_grid = param_grid
--> 821         _check_param_grid(param_grid)
    822 
    823     def fit(self, X, y=None):

/opt/conda/lib/python3.6/site-packages/sklearn/grid_search.py in _check_param_grid(param_grid)
    349             if True not in check:
    350                 raise ValueError("Parameter values for parameter ({0}) need "
--> 351                                  "to be a sequence.".format(name))
    352 
    353             if len(v) == 0:

ValueError: Parameter values for parameter (max_depth) need to be a sequence.

我是Python新手,不知道这个错误告诉我什么。这个方法期望的最大深度是多少?它说它在寻找一个序列,但是一个序列是什么??查斯?积分?在


Tags: thetoforsearchdatamodelparamsklearn