15

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

from sklearn import cross_validation

later in the code:

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

Error:

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
2
  • Post the code you're trying please, and the full traceback of the error Oct 26, 2018 at 15:21
  • Are you thinking of cross_val_score?
    – wbadart
    Oct 26, 2018 at 16:05

4 Answers 4

16

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)
8

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
3

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.