Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

spyder

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)
like image 523
Rejo R Varghese Avatar asked Oct 24 '25 17:10

Rejo R Varghese


1 Answers

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
like image 145
U12-Forward Avatar answered Oct 26 '25 05:10

U12-Forward