如何修复 python 中的“名称 'cross_validation' 未定义”错误

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

我正在尝试 运行 XGBClassifier 参数调整并在这行代码之后得到“'name 'cross_validation' is not defined”错误:

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

可能我没有导入合适的库?

首先,获取您的版本:

import sklearn
sklearn.__version__

scikit-learn 0.17版本后,cross_validation.KFold已经迁移到model_selection.KFold

如果你有0.17版本,使用这个:

from sklearn.cross_validation import KFold

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

如果您的版本高于 0.17,请使用:

from sklearn.model_selection import KFold

kfold_5 = KFold(n_splits = numFolds, shuffle=True)

Documentation for 0.21 version is here