265

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?

3
  • perhaps your module name is wrong if you have installed sklearn and anaconda correctly.
    – CKM
    Commented Oct 11, 2016 at 9:52
  • 16
    I am really just repeating it, but you have to use sklearn.model_selection from now on. cross_validation is not usable since sklearn 20+ Commented Mar 23, 2019 at 18:51
  • Wow 13 answers to say the same thing. The latest 4 years after the first one (in case bits would fade with time I suppose)
    – mins
    Commented Jan 9, 2021 at 18:12

15 Answers 15

822

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

2
  • 8
    Why didn't sklearn developers put in an alias for backward compatibility? Also, the doc for that older version should indicate this refactor: scikit-learn.org/0.16/modules/generated/….
    – flow2k
    Commented Aug 15, 2019 at 22:23
  • 1
    Because making code inoperable with API changes is one of the favorite features of python-based data science ;-) (See Pandas decade of version 0.x changes) Commented Nov 12, 2021 at 14:52
189

train_test_split is now in model_selection. Just type:

from sklearn.model_selection import train_test_split

it should work

45

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.

1
  • 1
    This is the Answer. You are awesome. Commented Apr 24, 2019 at 20:55
36

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
6
  • No, I don't have it, I need to install Anaconda?
    – arthurckl
    Commented Jun 5, 2015 at 13:21
  • Well,I would suggest you to install it.But You can also try sudo apt-get install python-sklearn,that should work. Commented Jun 5, 2015 at 13:30
  • 7
    The answer by @DimaLituiev is really the correct answer here. This issue is mostly related to a renaming, not a true missing package. Commented Feb 8, 2019 at 21:47
  • 3
    This isn't necessarily needed. You are suggesting installing a new environment just for one package that was renamed, I would say this solution is an overkill. Dima's solution should be marked as correct answer Commented Mar 21, 2019 at 14:35
  • I guess the question should have addressed if he has "import sklearn" worked or not first. If it works, maybe this is not a naming issue.
    – ju.
    Commented Apr 1, 2019 at 17:30
30

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.

24
sklearn.cross_validation

has changed to

sklearn.model_selection

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

17

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

2
  • 2
    It appears your answer is the same as a previous given in the same thread. If you think that answer can be improved, consider editing it instead of making a new answer to the same question.
    – Johan
    Commented Nov 4, 2018 at 14:40
  • @Johan Some how I did not see the other answer. I could delete my answer if you prefer.
    – nantitv
    Commented Nov 4, 2018 at 23:15
13

Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split
1
  • 2
    Same solution as ayat ullah sony's answer.
    – Eric Aya
    Commented Feb 10, 2019 at 18:58
10

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

10

sklearn.cross_validation is now changed to sklearn.model_selection

Just change

sklearn.cross_validation

to

sklearn.model_selection
10

The cross_validation is not available anymore.

Try to use model_selection instead of cross_validation:

from sklearn.model_selection import train_test_split
8

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
5

change the code like this

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

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
3

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

1
  • try adding more explanation to your answer to give it more value.
    – Zeina
    Commented Sep 19, 2019 at 10:29

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