关于python:ImportError:无法导入名称cross_validation

ImportError: cannot import name cross_validation

我无法从sklearn库导入交叉验证;我使用sklearn版本0.20.0

1
from sklearn import cross_validation

代码后面部分:

1
features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(word_data, authors, test_size=0.1, random_state=42)

错误:

1
2
3
4
5
6
7
8
Traceback (most recent call last):
  File"D:\me\M.Sc\Udacity_ML_course\ud120-projects-  master
aive_bayes
b_author_id.py"
, line 16, in <module>
    from email_preprocess import preprocess
  File"../tools/email_preprocess.py", line 8, in <module>
    from sklearn import cross_validation
ImportError: cannot import name cross_validation


cross_validation以前是作为scikit包存在的*,但在某些时候被弃用。

如果您要查找代码所示的train_test_split,则它位于model_selection中:

1
2
3
4
from sklearn import model_selection

features_train, features_test, labels_train, labels_test = model_selection.train_test_split(
    word_data, authors, test_size=0.1, random_state=42)

>看起来这在0.18中发生了变化。


这是因为sklearn中没有cross_validation对象。你很可能在寻找更像cross_validate函数的东西。你可以通过

1
from sklearn.model_selection import cross_validate

但是,您不需要导入任何交叉验证软件来执行列车测试分割,因为这只是从数据中随机抽样。尝试

1
from sklearn.model_selection import train_test_split

然后

1
features_train, features_test, labels_train, labels_test = train_test_split(word_data, authors, test_size=0.1, random_state=42)