Rejo R Varghese
Rejo R Varghese

Reputation: 61

import error for -cannot import name 'train_test_split' from 'sklearn.preprocessing'

I wanted to import train_test_split to split my dataset into a test dataset and a training dataset but an import error has occurred.

I tried all of these but none of them worked:

conda upgrade scikit-learn
pip uninstall scipy
pip3 install scipy

pip uninstall sklearn
pip uninstall scikit-learn
pip install sklearn

Here is the code which yields the error:

from sklearn.preprocessing import train_test_split
X_train, X_test, y_train, y_test = 
train_test_split(X,y,test_size=0.2,random_state=0)

And here is the error:

from sklearn.preprocessing import train_test_split
Traceback (most recent call last):

  File "<ipython-input-3-e25c97b1e6d9>", line 1, in <module>
    from sklearn.preprocessing import train_test_split

ImportError: cannot import name 'train_test_split' from 'sklearn.preprocessing' (C:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\__init__.py)

Upvotes: 4

Views: 27556

Answers (3)

The train_test_split is not under the preprocessing module it is under the model_selection module.

from sklearn.model_selection import train_test_split

Upvotes: 0

Luicfer Ai
Luicfer Ai

Reputation: 66

test_train_split is not present in preprocessing. It is present in model_selection module so try.

from sklearn.model_selection import train_test_split

it will work.

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71560

train_test_split isn't in preprocessing, it is in model_selection and cross_validation, so you meant:

from sklearn.model_selection import train_test_split

Or:

from sklearn.cross_validation import train_test_split

Upvotes: 12

Related Questions