【问题标题】:ImportError: cannot import name cross_validationImportError:无法导入名称 cross_validation
【发布时间】:2019-03-31 09:24:37
【问题描述】:

我无法从 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

【问题讨论】:

  • 请发布您正在尝试的代码,以及错误的完整追溯
  • 你在想cross_val_score吗?

标签: python scikit-learn


【解决方案1】:

这是因为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)

【讨论】:

    【解决方案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 中发生了变化。

    【讨论】:

      【解决方案3】:

      在我的例子中,我使用了 Udacity 课程中的一些文件,该课程使用了旧版本的 sklearn。 而不是花费不必要的时间重新格式化代码使用以满足所有依赖项的最新版本,安装旧版本更容易。

      这是可能的,因为他们提供了一个 requirements.txt 文件。

      python -m pip install -r requirements.txt

      【讨论】:

        【解决方案4】:

        在我的情况下,我还尝试安装旧版本的 sklearn,这是 'Intro to Machine Learning' Udacity 课程的迷你项目所必需的。

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

        不幸的是,@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
            ...
        

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

        conda install -c free scikit-learn=0.18.0
        

        【讨论】:

          猜你喜欢
          • 2019-05-27
          • 2019-10-04
          • 1970-01-01
          • 2016-03-31
          • 2014-10-10
          • 2014-09-20
          • 2014-08-28
          • 2014-06-10
          • 2016-05-16
          相关资源
          最近更新 更多