ImportError: cannot import name cross_validation

后端 未结 4 1124
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 11:50

I cannot import the cross_validation from sklearn library; I use sklearn version 0.20.0

from sklearn import cross_validation

later in the c

相关标签:
4条回答
  • 2020-12-03 12:27

    In my case I was also trying to install an old version of sklearn which is required for mini projects for 'Intro to Machine Learning' Udacity course.

    I use Miniconda 3 with Python 2 environment on Windows 10.

    Unfortunately, @Ben B's method with pip didn't work for me. I had errors which look like errors in the 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
        ...
    

    So I tried another method with conda described in the following answer:

    conda install -c free scikit-learn=0.18.0
    
    0 讨论(0)
  • 2020-12-03 12:34

    In my case, I was using some files from a Udacity course, which used an older version of sklearn. Instead of spending unnecessary time reformatting code usage to meet the latest versions of all their dependencies, it was easier to install the old version.

    This was possible because they provide a requirements.txt file.

    python -m pip install -r requirements.txt

    0 讨论(0)
  • 2020-12-03 12:45

    cross_validation used to exist as a Scikit package*, but was deprecated at some point.

    If you're looking for train_test_split as your code indicates, it's in 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)
    

    *Looks like this changed in 0.18.

    0 讨论(0)
  • 2020-12-03 12:46

    This happens because there is no cross_validation object in sklearn. You're likely looking for something more like the cross_validate function. You can access that through

    from sklearn.model_selection import cross_validate
    

    However, you don't need to import any cross-validation software to perform the train-test split, since that will just randomly sample from the data. Try

    from sklearn.model_selection import train_test_split
    

    followed by

    features_train, features_test, labels_train, labels_test = train_test_split(word_data, authors, test_size=0.1, random_state=42)
    
    0 讨论(0)
提交回复
热议问题