简体   繁体   中英

Why I got an TypeError: __init__() got an unexpected keyword argument 'outer_cv' in python sklearn?

I have run the example code in the link: https://github.com/casperbh96/Nested-Cross-Validation/blob/master/Example%20Notebook%20-%20NestedCV.ipynb , but one error was got: init () got an unexpected keyword argument 'outer_cv', I have check the source code, the 'outer_cv' was included in the int (), how to solve it? The example code also pasted as following:

from nested_cv import NestedCV

import pandas as pd
import numpy as np
from sklearn.datasets import load_boston, load_iris, load_breast_cancer
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.model_selection import KFold

# When using Random Search, we get a user warning with this little number of hyperparameters
# Suppress it
import warnings
warnings.simplefilter(action='ignore', category=UserWarning)
warnings.simplefilter(action='ignore', category=FutureWarning)

boston = load_boston()
X = boston.data
y = boston.target

# Define a parameters grid
param_grid = {
     'max_depth': [3, 7, 10, None],
     'n_estimators': [100,200],
     'min_samples_split':[2,3,5,7,10]
}

# Either specify a strategy or number
# Here we choose a strategy
outer_cv = KFold(n_splits=5,
                 shuffle=True,
                 random_state=123)
inner_cv = KFold(n_splits=5,
                 shuffle=True,
                 random_state=123)

NCV = NestedCV(model=RandomForestRegressor(), params_grid=param_grid,
               outer_cv=outer_cv, inner_cv=inner_cv, n_jobs = -1,
               cv_options={'sqrt_of_score':True, 
                           'recursive_feature_elimination':False, 
                           'rfe_n_features':2})
NCV.fit(X=X,y=y)

NCV.outer_scores

This should get you closer, though i can't get NCV.fit to finish in any reasonable time:

from nested_cv import NestedCV

import pandas as pd
import numpy as np
from sklearn.datasets import load_boston, load_iris, load_breast_cancer
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.model_selection import KFold


# When using Random Search, we get a user warning with this little number of hyperparameters
# Suppress it
import warnings
warnings.simplefilter(action='ignore', category=UserWarning)
warnings.simplefilter(action='ignore', category=FutureWarning)

boston = load_boston()
X = boston.data
y = boston.target

# Define a parameters grid
param_grid = {
     'max_depth': [3, 7, 10, None],
     'n_estimators': [100,200],
     'min_samples_split':[2,3,5,7,10]
}

# Either specify a strategy or number
# Here we choose a strategy
outer_cv = KFold(n_splits=5,
                 shuffle=True,
                 random_state=123)
inner_cv = KFold(n_splits=5,
                 shuffle=True,
                 random_state=123)


NCV = NestedCV(model=RandomForestRegressor(), params_grid=param_grid,
               outer_kfolds=outer_cv.n_splits, inner_kfolds=inner_cv.n_splits, n_jobs = -1,
               cv_options={'sqrt_of_score':True, 
                           'recursive_feature_elimination':False, 
                           'rfe_n_features':2})
#NCV.fit(X=X,y=y)

NCV.outer_scores

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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