繁体   English   中英

TypeError:__init __()为参数'n_splits'获取了多个值

[英]TypeError: __init__() got multiple values for argument 'n_splits'

我正在使用以下版本的SKLearn版本(0.20.2):

from sklearn.model_selection import StratifiedKFold


grid = GridSearchCV(
    pipeline,  # pipeline from above
    params,  # parameters to tune via cross validation
    refit=True,  # fit using all available data at the end, on the best found param combination
    scoring='accuracy',  # what score are we optimizing?
    cv=StratifiedKFold(label_train, n_splits=5),  # what type of cross validation to use
)

但是我不明白为什么我会得到这个错误:


TypeError                                 Traceback (most recent call last)
<ipython-input-26-03a56044cb82> in <module>()
     10     refit=True,  # fit using all available data at the end, on the best found param combination
     11     scoring='accuracy',  # what score are we optimizing?
---> 12     cv=StratifiedKFold(label_train, n_splits=5),  # what type of cross validation to use
     13 )

TypeError: __init__() got multiple values for argument 'n_splits'

我已经尝试过n_fold但结果相同。 并且也厌倦了更新我的scikit版本和我的conda。 有解决这个问题的主意吗? 非常感谢!

初始化时StratifiedKFold恰好接受3个参数,都不是训练数据:

StratifiedKFold(n_splits='warn', shuffle=False, random_state=None)

因此,当您调用StratifiedKFold(label_train, n_splits=5)它会认为您两次传递了n_splits

而是创建对象,然后使用sklearn docs页面上的示例中所述的方法使用对象拆分数据:

get_n_splits([X,y,groups])返回交叉验证程序中的分割迭代次数split(X,y [,groups])生成索引以将数据分割为训练和测试集。

StratifiedKFold接受三个参数,但是您要传递两个参数。 在sklearn 文档中查看更多

创建StratifiedKFold对象,并将其传递给GridSearchCV,如下所示。

skf = StratifiedKFold(n_splits=5)
skf.get_n_splits(X_train, Y_train)

grid = GridSearchCV(
pipeline,  # pipeline from above
params,  # parameters to tune via cross validation
refit=True,  # fit using all available data at the end, on the best found param combination
scoring='accuracy',  # what score are we optimizing?
cv=skf,  # what type of cross validation to use
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM