关于python:TypeError:__init __()为参数’n_splits’获得了多个值

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'

我已经尝试过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]) 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
)