[Solved] ImportError: cannot import name cross_validation

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)


Traceback (most recent call last):
  File "D:meM.ScUdacity_ML_courseud120-projects-", line 16, in <module>
    from email_preprocess import preprocess
  File "../tools/", line 8, in <module>
    from sklearn import cross_validation
ImportError: cannot import name cross_validation
Enquirer: mnabil


Solution #1:

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)
Respondent: mnabil

Solution #2:

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.

Respondent: jwil

Solution #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

Respondent: Brad Solomon

Solution #4:

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 install for scipy ... error
    Complete output from command "c:program filespython2.xpython.exe" -u -c "import setuptools, tokenize;__file__='c:\users\reacodes\appdata\local\temp\pip-build-jzv_lz\scipy\';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('rn', 'n'), __file__, 'exec'))" install --record c:usersreacodesappdatalocaltemppip-mqeonc-recordinstall-record.txt
--single-version-externally-managed --compile:
      libraries openblas not found in ['c:\program files\python\2.x\lib', 'C:\', 'c:\program files\python\2.x\libs']

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

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

conda install -c free scikit-learn=0.18.0
Respondent: Ben Butterworth

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy