【问题标题】:Parameter values for parameter (n_estimators) need to be a sequence参数(n_estimators)的参数值需要是一个序列
【发布时间】:2018-01-08 17:47:48
【问题描述】:

我收到了错误,不知道如何修复它。你能帮忙吗?完整代码可以在https://github.com/kthouz/NYC_Green_Taxi/blob/master/NYC%20Green%20Taxi.ipynb找到

通过网格搜索优化 n_estimator

def optimize_num_trees(alg,param_test,scoring_method,train,predictors,target):
    """
    This functions is used to tune paremeters of a predictive algorithm
    alg: sklearn model,
    param_test: dict, parameters to be tuned
    scoring_method: str, method to be used by the cross-validation to valuate the model
    train: pandas.DataFrame, training data
    predictors: list, labels to be used in the model training process. They should be in the column names of dtrain
    target: str, target variable
    """
    gsearch = GridSearchCV(estimator=alg, param_grid = param_test, scoring=scoring_method,n_jobs=2,iid=False,cv=5)
    gsearch.fit(train[predictors],train[target])
    return gsearch

# get results of the search grid
gs_cls = optimize_num_trees(model_cls,param_test,'roc_auc',train,predictors,target)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-42-c7419a90cdb1> in <module>()
      1 
      2 # get results of the search grid
----> 3 gs_cls = optimize_num_trees(model_cls,param_test,'roc_auc',train,predictors,target)
      4 

<ipython-input-40-2b76f2ffb87f> in optimize_num_trees(alg, param_test, scoring_method, train, predictors, target)
     57     target: str, target variable
     58     """
---> 59     gsearch = GridSearchCV(estimator=alg, param_grid = param_test, scoring=scoring_method,n_jobs=2,iid=False,cv=5)
     60     gsearch.fit(train[predictors],train[target])
     61     return gsearch

/Users/anaconda/lib/python3.5/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)
    810             refit, cv, verbose, pre_dispatch, error_score)
    811         self.param_grid = param_grid
--> 812         _check_param_grid(param_grid)
    813 
    814     def fit(self, X, y=None):

/Users/anaconda/lib/python3.5/site-packages/sklearn/grid_search.py in _check_param_grid(param_grid)
    346             if True not in check:
    347                 raise ValueError("Parameter values for parameter ({0}) need "
--> 348                                  "to be a sequence.".format(name))
    349 
    350             if len(v) == 0:

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

【问题讨论】:

  • 检查param_test 变量。看看你是否正确初始化了它。它应该是字典或字典列表。您添加的链接将 param_test 声明为字典。 param_test = {'n_estimators':range(50,200,25)}。如果需要列表而不是生成器,您也可以尝试使用 param_test = {'n_estimators':list(range(50,200,25))}
  • 你能看懂吗?我不能……
  • 编辑您的问题并使用编辑框上方的{} 按钮将您的代码格式化为代码,使其不会重排。
  • {'n_estimators':list(range(50,200,25))} 添加此修复它。谢谢!!!
  • @ClockSlave 发现了同样的问题,并且 `params = {'max_depth': list(range(1,11))}` 也帮助了我,谁能帮助我为什么会出现这个问题?我在写作中遗漏了什么,比如params = {'max_depth': range(1,11)}

标签: python


【解决方案1】:

我遇到了和你类似的错误:

ValueError: 参数 (warm_start) 的参数值需要是 序列(但不是字符串)或 np.ndarray。网站:stackoverflow.com

每个键的值显然需要在数组括号 []

我的错误代码:

params = {
    'max_depth': [11],
    'warm_start': True
}

我的正确代码:

params = {
    'max_depth': [11],
    'warm_start': [True]
}

【讨论】:

    【解决方案2】:

    我遇到了和你类似的错误,代码如下:

    # optimize n_estimator through grid search
    # define range over which number of trees is to be optimized
    param_test = {'n_estimators':range(30,151,20)} 
    

    你可以改变

    range(30,151,20)np.arange(30,151,20)

    【讨论】:

      【解决方案3】:

      GridSearchCV 需要序列格式的参数值,因此即使参数值是单个值,您也应该始终以列表或 numpy 数组的形式给出参数值。

      例如: 如果您为 GridSearchCV 提供以下字典,则会引发错误,因为 n_jobs -1 的值是单个整数而不是序列(列表或数组)。

      参数={'alpha':[0.01, 0.1, 1, 10], 'n_jobs':-1}

      但是如果您将 -1 包装在一个列表中并提供给 GridSearchCV 它不会引发任何错误。 参数={'alpha':[0.01,0.1,1,10], 'n_jobs': [-1]}

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2015-09-28
        • 2015-10-07
        • 1970-01-01
        • 2011-10-16
        • 2012-06-23
        • 2018-08-28
        相关资源
        最近更新 更多