Python:ValueError太多的值要解包(期望2个)

3
我正在尝试通过GridSearchCV找到最佳的xgboost模型,并且作为交叉验证,我想使用四月份的目标数据。以下是代码:
    x_train.head()

x_train

可以翻译为:

{{链接1:x_train}}


    y_train.head()

y_train

可能是一个包含链接的 HTML 代码段。但是由于上下文不明确,我无法确定具体内容和翻译。请提供更多信息以获得更准确的翻译。
    from sklearn.model_selection import GridSearchCV
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error
    from sklearn.metrics import make_scorer
    from sklearn.ensemble import RandomForestRegressor
    from sklearn.model_selection import TimeSeriesSplit
    import xgboost as xg

    xgb_parameters={'max_depth':[3,5,7,9],'min_child_weight':[1,3,5]}
    xgb=xg.XGBRegressor(learning_rate=0.1, n_estimators=100,max_depth=5, min_child_weight=1, gamma=0, subsample=0.8, colsample_bytree=0.8)
    model=GridSearchCV(n_jobs=2,estimator=xgb,param_grid=xgb_parameters,cv=train_test_split(x_train,y_train,test_size=len(y_train['2016-04':'2016-04']), random_state=42, shuffle=False),scoring=my_func)
    model.fit(x_train,y_train)
    model.grid_scores_
    model.best_params_

但是在我训练模型时出现了这个错误。 错误 请问有人能帮我解决这个问题吗?或者有人能建议我如何将未洗牌的数据拆分为训练/测试集,以便在最后一个月验证模型吗?
谢谢帮助。

请检查代码的这一部分:test_size=len(y_train['2016-04':'2016-04']) - Ajay
@Ajay,但是这部分有什么问题我看不到...我只是在这里写一个测试大小的长度。 - Nikita Okorokov
1个回答

6
这个错误的根本原因是你在GridSearchCV()函数调用中使用cv参数的方式不正确:
cv=train_test_split(x_train,y_train,test_size=len(y_train['2016-04':'2016-04'])

下面是 cv 参数的文档字符串摘录:

指定要使用的交叉验证策略。默认为 None,使用 5 折交叉验证。

cv : int, cross-validation generator or an iterable, optional
    Determines the cross-validation splitting strategy.
    Possible inputs for cv are:
      - None, to use the default 3-fold cross validation,
      - integer, to specify the number of folds in a `(Stratified)KFold`,
      - An object to be used as a cross-validation generator.
      - An iterable yielding train, test splits.

    For integer/None inputs, if the estimator is a classifier and ``y`` is
    either binary or multiclass, :class:`StratifiedKFold` is used. In all
    other cases, :class:`KFold` is used.

    Refer :ref:`User Guide <cross_validation>` for the various
    cross-validation strategies that can be used here.

然而train_test_split(x_train,y_train)返回4个数组:

X_train, X_test, y_train, y_test

这会导致错误:ValueError too many values to unpack (expected 2)

解决方法是可以指定上述其中一个选项(cv参数的文档字符串)...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接