簡體   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