导入错误:无法导入cross_validation。

2024-05-15 09:36:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

from sklearn import cross_validation

代码的后面部分:

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

错误:

Traceback (most recent call last):
  File "D:\me\M.Sc\Udacity_ML_course\ud120-projects-  master\naive_bayes\nb_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

Tags: infrompytestimportlabelslinetrain
2条回答

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

如果您正在寻找代码指示的train_test_split,则它位于model_selection

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函数的东西。你可以通过

from sklearn.model_selection import cross_validate

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

from sklearn.model_selection import train_test_split

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

相关问题 更多 >