2

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?

Erez Ben-Moshe
  • 149
  • 2
  • 3
  • 10

1 Answers1

2

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

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • The import statement depends on the OP's sklearn version. I have updated my answer. – seralouk Jul 27 '19 at 16:07
  • Indeed it is - (+1) now (previous versions of the answer assumed that OP had a new version, while such info is not provided) – desertnaut Jul 27 '19 at 16:07
  • @serafeim For the first code snippet I get "No module named 'sklearn.cross_validation' For the 2nd I get "TypeError: __init__() got an unexpected keyword argument 'n'" – Erez Ben-Moshe Jul 27 '19 at 16:33
  • for the second, did you type exactly `kfold_5 = KFold(n_splits = numFolds, shuffle=True)` ? The input arguments are not the same as in the first case, in this case. – seralouk Jul 27 '19 at 16:36
  • @ErezBen-Moshe please pay close attention to the provided snippets; in the 2nd snippet there should **not** be any argument `n` – desertnaut Jul 27 '19 at 17:57