ImportError:无法导入名称cross_validation

时间:2018-10-26 15:15:58

标签: 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

4 个答案:

答案 0 :(得分:4)

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中发生了变化。

答案 1 :(得分:3)

发生这种情况是因为cross_validation中没有sklearn对象。您可能正在寻找更像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)

答案 2 :(得分:3)

以我为例,我使用的是Udacity课程中的一些文件,该课程使用的是sklearn的旧版本。与其花费不必要的时间来重新格式化代码用法以满足所有依赖关系的最新版本,较容易安装旧版本。

之所以可行是因为他们提供了requirements.txt文件。

python -m pip install -r requirements.txt

答案 3 :(得分:0)

就我而言,我还尝试安装旧版本的sklearn,这是'Intro to Machine Learning' Udacity课程的小型项目所必需的。

我在Windows 10上的Python 2环境中使用Miniconda 3。

不幸的是,@ Ben B的pip方法对我不起作用。我遇到了类似scipy github issue中的错误的错误:

Installing collected packages: scipy   Running setup.py install for scipy ... error
    Complete output from command "c:\program files\python\2.x\python.exe" -u -c "import setuptools, tokenize;__file__='c:\\users\\reacodes\\appdata\\local\\temp\\pip-build-jzv_lz\\scipy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record c:\users\reacodes\appdata\local\temp\pip-mqeonc-record\install-record.txt
--single-version-externally-managed --compile:
    lapack_opt_info:
    openblas_lapack_info:
      libraries openblas not found in ['c:\\program files\\python\\2.x\\lib', 'C:\\', 'c:\\program files\\python\\2.x\\libs']
      NOT AVAILABLE

    lapack_mkl_info:
    mkl_info:
      libraries mkl,vml,guide not found in ['c:\\program files\\python\\2.x\\lib', 'C:\\', 'c:\\program files\\python\\2.x\\libs']
      NOT AVAILABLE
    ...

因此,我尝试了以下answer中所述的另一种使用conda的方法:

conda install -c free scikit-learn=0.18.0
相关问题