Nameerror: name ‘train_test_split’ is not defined ( Solved )

Nameerror name 'train_test_split' is not defined

The scikit-learn is a python package that is open source and mainly used for designing the predictive model. It has many machine learning algorithms like clustering, regression, classification e.t.c. You can import all these algorithms from sklearn module. If you are getting the nameerror: name ‘train_test_split’ is not defined error then this post is for you. In this entire tutorial, you will know how to solve the issue of the name ‘train_test_split’ is not defined.

What is train_test_split?

The train_test_split allows you to divide the datasets into two parts. One part is used for training purposes and the other part is for testing purposes. The train part dataset allows you to build or design a predictive model and the test part is used to check the accuracy of the model.

The root cause of nameerror: name ‘train_test_split’ is not defined Error

There can be many causes for the nameerror: name ‘train_test_split’ is not defined error. In this section, you will know all about them.

Case 1: sklearn module is not installed

The first case for getting this error is that you have not installed the sklearn package in your system. You can check using the below code.

import sklearn
print(sklearn.__version__)
ModuleNotFoundError: no module named 'sklearn' 

Solution

If you not getting the version of the sklearn in your system and getting the no module named sklearn error. Then it’s obvious that the sklearn is not installed in your system. You have to use the below command to install it.

pip3 install sklearn
Installing sklearn
Installing sklearn

Case 2:  Not properly importing the train_test_split module

In the second case when you are getting “nameerror: name ‘train_test_split’ is not defined error is because you are not properly importing the train_split_test module like below.

from sklearn.cross_validation import train_test_split

Output

Not properly importing train_test_split module
Not properly importing train_test_split module

Solution

The solution to this is very simple. Instead of importing the train_test_split module from sklearn.cross_validation ,import it from sklearn.model_selection . You will not get the error.

Importing train_test_split from sklearn model selection
Importing train_test_split from sklearn model selection

Conclusion

The  train_test_split module is very useful for designing the machine learning model. If you are getting the name ‘train_test_split’ is not defined error then the above method will solve your problem.

I hope you have liked this tutorial. If you have any doubt or queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner