ImportError: No module named sklearn.cross_validation

PythonScikit Learn

Python Problem Overview


I am using python 2.7 in Ubuntu 14.04. I installed scikit-learn, numpy and matplotlib with these commands:

sudo apt-get install build-essential python-dev python-numpy \
python-numpy-dev python-scipy libatlas-dev g++ python-matplotlib \
ipython

But when I import these packages:

from sklearn.cross_validation import train_test_split

It returns me this error:

ImportError: No module named sklearn.cross_validation

What I need to do?

Python Solutions


Solution 1 - Python

It must relate to the renaming and deprecation of cross_validation sub-module to model_selection. Try substituting cross_validation to model_selection

Solution 2 - Python

train_test_split is now in model_selection. Just type:

from sklearn.model_selection import train_test_split

it should work

Solution 3 - Python

I guess cross selection is not active anymore. We should use instead model selection. You can write it to run, from sklearn.model_selection import train_test_split

Thats it.

Solution 4 - Python

Make sure you have Anaconda installed and then create a virtualenv using conda. This will ensure all the imports work

Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar  9 2015, 16:20:48) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> from sklearn.cross_validation import train_test_split

Solution 5 - Python

sklearn.cross_validation is now changed to sklearn.model_selection

Just use

from sklearn.model_selection import train_test_split

I think that will work.

Solution 6 - Python

sklearn.cross_validation

has changed to

sklearn.model_selection

Checkout the documentation here: https://scikit-learn.org/stable/modules/cross_validation.html

Solution 7 - Python

May be it's due to the deprecation of sklearn.cross_validation. Please replace sklearn.cross_validation with sklearn.model_selection

Ref- https://github.com/amueller/scipy_2015_sklearn_tutorial/issues/60

Solution 8 - Python

Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split

Solution 9 - Python

Past : from sklearn.cross_validation (This package is deprecated in 0.18 version from 0.20 onwards it is changed to from sklearn import model_selection).

Present: from sklearn import model_selection

Example 2:

Past : from sklearn.cross_validation import cross_val_score (Version 0.18 which is deprecated)

Present : from sklearn.model_selection import cross_val_score

Solution 10 - Python

sklearn.cross_validation is now changed to sklearn.model_selection

Just change

sklearn.cross_validation

to

sklearn.model_selection

Solution 11 - Python

If you have code that needs to run various versions you could do something like this:

import sklearn
if sklearn.__version__ > '0.18':
    from sklearn.model_selection import train_test_split
else:
    from sklearn.cross_validation import train_test_split

This isn't ideal though because you're comparing package versions as strings, which usually works but doesn't always. If you're willing to install packaging, this is a much better approach:

from packaging.version import parse
import sklearn
if parse(sklearn.__version__) > parse('0.18'):
    from sklearn.model_selection import train_test_split
else:
    from sklearn.cross_validation import train_test_split

Solution 12 - Python

change the code like this

# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split

Solution 13 - Python

train_test_split is part of the module sklearn.model_selection, hence, you may need to import the module from model_selection

Code:

from sklearn.model_selection import train_test_split

Solution 14 - Python

cross_validation was deprecated some time ago, try switching it out with model_selection

Solution 15 - Python

The cross_validation is not available anymore.

Try to use model_selection instead of cross_validation:

from sklearn.model_selection import train_test_split

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionarthurcklView Question on Stackoverflow
Solution 1 - PythonDima LituievView Answer on Stackoverflow
Solution 2 - Pythonayat ullah sonyView Answer on Stackoverflow
Solution 3 - PythonMurat ŞahinView Answer on Stackoverflow
Solution 4 - PythonRichard RublevView Answer on Stackoverflow
Solution 5 - PythonTanmoy BhowmickView Answer on Stackoverflow
Solution 6 - PythonAsadView Answer on Stackoverflow
Solution 7 - PythonnantitvView Answer on Stackoverflow
Solution 8 - PythonoptimistsView Answer on Stackoverflow
Solution 9 - Pythonbaya prakash reddyView Answer on Stackoverflow
Solution 10 - PythonSani KamalView Answer on Stackoverflow
Solution 11 - Pythonjss367View Answer on Stackoverflow
Solution 12 - PythonMuhammad ShabinView Answer on Stackoverflow
Solution 13 - PythonDecision ScientistView Answer on Stackoverflow
Solution 14 - PythonAniket MukherjeeView Answer on Stackoverflow
Solution 15 - PythonAbhishekView Answer on Stackoverflow