scikit-learncross-validationxgbclassifier

How to fix 'name 'cross_validation' is not defined' error in python


I am trying to run XGBClassifier parameter tuning and get a "'name 'cross_validation' is not defined" error following this line of code:

  kfold_5 =  cross_validation.KFold(n = len(X), shuffle = True, n_folds = numFolds)

Maybe I didn't import the appropriate library?


Solution

  • First, get your version:

    import sklearn
    sklearn.__version__
    

    After scikit-learn version 0.17, the cross_validation.KFold has been migrated to model_selection.KFold.

    If you have the 0.17 version, use this:

    from sklearn.cross_validation import KFold
    
    kfold_5 = KFold(n= len(X), n_folds = numFolds, shuffle=True)
    

    If you have a version newer than 0.17, use this:

    from sklearn.model_selection import KFold
    
    kfold_5 = KFold(n_splits = numFolds, shuffle=True)
    

    Documentation for 0.21 version is here