ImportError: no module named sklearn.cross_validation

I am using Python 2.7 on Ubuntu 14.04. I installed scikit-learn, numpy and matplotlib using 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 

This returns me this error:

 ImportError: No module named sklearn.cross_validation 

What should I do?

+93
python scikit-learn
source share
12 answers

Make sure you have Anaconda installed and then create virtualenv using conda . This will ensure that all 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 
+2
source share

it should relate to renaming and cross_validation submodule model_selection to model_selection . Try replacing cross_validation model_selection

+355
source share

train_test_split is now in model_selection. Just enter:

 from sklearn.model_selection import train_test_split 

he should work

+100
source share

I think cross-selection is no longer active. We should use instead of choosing a model. You can write it to run, from sklearn.model_selection import train_test_split

This is it.

+38
source share
 sklearn.cross_validation 

changed to

 sklearn.model_selection 

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

+18
source share

sklearn.cross_validation now changed to sklearn.model_selection

Just use

 from sklearn.model_selection import train_test_split 

I think this will work.

+18
source share

Maybe this is due to obsolescence sklearn.cross_validation. Please replace sklearn.cross_validation with sklearn.model_selection

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

+11
source share

Dividing a data set into a training set and a test set

 from sklearn.model_selection import train_test_split 
+11
source share

Past: from sklearn.cross_validation (This package has been deprecated in version 0.18 since version 0.20, it has been replaced by from sklearn import model_selection ).

Gift: from sklearn import model_selection

Example 2:

Past: from sklearn.cross_validation import cross_val_score (version 0.18 deprecated)

Present: from sklearn.model_selection import cross_val_score

+8
source share
 Older Version: from sklearn.cross_validation import train_test_split Present Version from sklearn.model_selection import train_test_split 
0
source share

cross_validation has been deprecated a while ago, try disabling it with model_selection

0
source share

sklearn.cross_validation now changed to sklearn.model_selection

Just change

 sklearn.cross_validation 

in

 sklearn.model_selection 
0
source share

All Articles