97

I am trying to use train_test_split function and write:

from sklearn.model_selection import train_test_split

and this causes

ImportError: No module named model_selection

Why? And how to overcome?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 2
    It just occured to me, that you've not accepted any answers in at least your last ten questions. Remember, accepting the most helpful answer will help the answerer and people looking on your question in the future. – linusg Nov 20 '16 at 13:35

11 Answers11

191

I guess you have the wrong version of scikit-learn, a similar situation was described here on GitHub. Previously (before v0.18), train_test_split was located in the cross_validation module:

from sklearn.cross_validation import train_test_split

However, now it's in the model_selection module:

from sklearn.model_selection import train_test_split

so you'll need the newest version.

To upgrade to at least version 0.18, do:

pip install -U scikit-learn

(Or pip3, depending on your version of Python). If you've installed it in a different way, make sure you use another method to update, for example when using Anaconda.

linusg
  • 6,289
  • 4
  • 28
  • 78
  • 7
    This didn't work for me, however Farhard Maleki's solution, "conda update scikit-learn" did. – Alex Kinman Jul 06 '17 at 18:28
  • I ran the pip command you gave, and it would throw an error asking me to upgrade pip. Then I upgraded pip and reran, and it would tell that there was no need to upgrade sklearn, I already have the latest version (0.17) even thought hat wasn't the case. Not trying to haggle, just trying to share info. – Alex Kinman Jul 06 '17 at 19:12
  • @AlexKinman If you have Python 2 _and_ 3 installed, make sure to use the "right pip" command. Are you sure you don't have venvs or are in the right one? This sounds strange, could you open a Python shell and put in ``import sklearn``, ``sklearn`` (yep, just the word) and ``sklearn.__version__``? Even though it is fixed for you, it would be interesting to know where the issue came from. – linusg Jul 06 '17 at 19:17
  • sklearn Out[2]: sklearn.__version__ Out[3]: '0.18.2' *But this is after I've run the Conda upgrade of sklearn* – Alex Kinman Jul 06 '17 at 19:28
  • Anyhow, good it works for you now. I have not used Anaconda so much, but I guess it does not integrate with pip. Answer edited, all Anaconda users will know :) – linusg Jul 06 '17 at 19:34
26

Update sklearn:

conda update scikit-learn
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Farhad Maleki
  • 3,451
  • 1
  • 25
  • 20
9

I encountered this problem when I import GridSearchCV.

Just changed sklearn.model_selection to sklearn.grid_search.

jwvh
  • 50,871
  • 7
  • 38
  • 64
curry_xyd
  • 101
  • 1
7

I had the same problem while using Jupyter Notebook, no matter what I updated in Python 3, conda, I could not get in Jupyter:

import sklearn
print (sklearn.__version__)
0.17.1

to SHOW scikit-learn-0.18.1

Finally, I removed Anaconda3 and Jupyter Notebook and reinstalled fresh. I got it to work.

http://ukitech.blogspot.com/2017/02/sklearnmodelselection.html

Uki D. Lucas
  • 516
  • 6
  • 4
5

Do you have sklearn?

If not, do the following:

sudo pip install sklearn

After installing sklearn:

from sklearn.model_selection import train_test_split

works fine.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Alex L
  • 1,069
  • 2
  • 18
  • 33
3

To install scikit-learn version 18.0, I used both commands:

conda update scikit-learn

pip install -U scikit-learn

But it does not work. There was a problem "Cannot install 'scikit-learn'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall".

Finally, i can install it by using following command:

pip install --user --upgrade scikit-learn==0.18.0

Minh Vo
  • 31
  • 3
3

In Late September 2016, SciKit Learn 0.18 was released and there was a slight change to the code. With SciKit Learn 0.18 the train_test_split function is now imported from model_selection instead of cross_validation.

from sklearn.cross_validation import train_test_split

has been changed to :

from sklearn.model_selection import train_test_split

The same has also happened for GridSearchCV.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Paras Patidar
  • 21
  • 1
  • 1
  • 6
2

As @linusg said, one option is just import crossvalidation as follows:

from sklearn import cross_validation
X_train,X_test,y_train,y_test = cross_validation.train_test_split(X,y,test_size=0.3)
MCardus
  • 38
  • 5
2

Latest Stable release of sklearn 0.20.0 has train_test_split under model_selection not under cross_validation.

In order to check your sklearn version:

import sklearn
print (sklearn.__version__)
0.20.2
1

Adding some info to the previous answer from @linusg :

sklearn keeps a release history of all its changes. Think of checking it from time to time. Here is the link to the documentation.

As you can see in the documentation for the version 0.18, a new module was created called model_selection. Therefore it didn't exist in previous versions.

Update sklearn and it will work !

MMF
  • 5,750
  • 3
  • 16
  • 20
0

Your sklearn version is too low, model_selection is imported by 0.18.1, so please update the sklearn version.

duan
  • 8,515
  • 3
  • 48
  • 70
Changyuan Chen
  • 308
  • 1
  • 3
  • 10