257

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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
arthurckl
  • 5,281
  • 6
  • 17
  • 16
  • perhaps your module name is wrong if you have installed sklearn and anaconda correctly. – CKM Oct 11 '16 at 09: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+ – Michal Mikuláši Mar 23 '19 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 Jan 09 '21 at 18:12

15 Answers15

804

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

The Bearded Llama
  • 3,036
  • 4
  • 20
  • 31
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58
  • 8
    Why didn't sklearn developers put in an alias for backward compatibility? Also, the doc for that older version should indicate this refactor: https://scikit-learn.org/0.16/modules/generated/sklearn.cross_validation.train_test_split.html. – flow2k Aug 15 '19 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) – Jeff Winchell Nov 12 '21 at 14:52
186

train_test_split is now in model_selection. Just type:

from sklearn.model_selection import train_test_split

it should work

Jee Mok
  • 6,157
  • 8
  • 47
  • 80
ayat ullah sony
  • 1,973
  • 1
  • 10
  • 7
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.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
MSAHIN
  • 451
  • 4
  • 4
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
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
  • No, I don't have it, I need to install Anaconda? – arthurckl Jun 05 '15 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. – Richard Rublev Jun 05 '15 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. – Amrinder Arora Feb 08 '19 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 – MuhsinFatih Mar 21 '19 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. Apr 01 '19 at 17:30
  • Note that you don't have to have Anaconda to fix this issue (I just use pip). See other answers for a possible fix. – Rushi Agrawal May 17 '19 at 00:31
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.

akshayk07
  • 2,092
  • 1
  • 20
  • 32
Tanmoy Bhowmick
  • 1,305
  • 15
  • 20
24
sklearn.cross_validation

has changed to

sklearn.model_selection

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

Asad
  • 930
  • 7
  • 10
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

nantitv
  • 3,539
  • 4
  • 38
  • 61
  • 2
    It appears your answer is the [same as a previous given](https://stackoverflow.com/a/34844352/10400050) in the same thread. If you think that answer can be improved, consider [editing](https://stackoverflow.com/posts/34844352/edit) it instead of making a new answer to the same question. – Johan Nov 04 '18 at 14:40
  • @Johan Some how I did not see the other answer. I could delete my answer if you prefer. – nantitv Nov 04 '18 at 23:15
13

Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
optimists
  • 191
  • 1
  • 7
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
Sani Kamal
  • 1,208
  • 16
  • 26
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
Ewran
  • 328
  • 4
  • 15
Abhishek
  • 121
  • 1
  • 4
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
jss367
  • 4,759
  • 14
  • 54
  • 76
5

change the code like this

# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
Muhammad Shabin
  • 381
  • 5
  • 5
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
David Buck
  • 3,752
  • 35
  • 31
  • 35
Noah Sheldon
  • 1,461
  • 10
  • 12
3

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