python - 导入错误 : cannot import name cross_validation

标签 python scikit-learn

我无法从 sklearn 库中导入 cross_validation;我使用 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

最佳答案

发生这种情况是因为 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)

关于python - 导入错误 : cannot import name cross_validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53011739/

相关文章:

python - 调整参数时识别交叉验证 SVM 中的过度拟合

python - 在 GridSearchCV 的管道中交替使用不同的模型

python-3.x - '损失: nan' during training of Neural Network in Keras

Python在一行代码上运行速度极慢

python - Numpy 基于时间的矢量操作,其中前面元素的状态很重要 - for 循环是否合适?

javascript - ajax 将数据从 javascript 发送到 python

python - 将信息从 html 传递给 python

python - 使用 PyODBC 将参数传递给存储过程

python - 网页抓取表格的内容

python - Sklearn SGDClassifier 部分拟合