TypeError: __init__() got multiple values for argument 'n_splits'
我正在使用以下版本的SKLearn版本(0.20.2):
1 2 3 4 5 6 7 8 9 10 | 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 ) |
但是我不明白为什么我会得到这个错误:
1 2 3 4 5 6 7 8 | 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' |
我已经尝试过
初始化时StratifiedKFold恰好接受3个参数,都不是训练数据:
因此,当您调用
而是创建对象,然后使用sklearn docs页面上的示例中所述的方法使用对象拆分数据:
get_n_splits([X, y, groups]) Returns the number of splitting
iterations in the cross-validator split(X, y[, groups]) Generate
indices to split data into training and test set.
StratifiedKFold接受三个参数,但是您要传递两个参数。 在sklearn文档中查看更多
创建StratifiedKFold对象,并将其传递给GridSearchCV,如下所示。
1 2 3 4 5 6 7 8 9 10 | 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 ) |