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.
ModuleNotFoundError: no module named 'sklearn'
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
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
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.
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.
Similar Error :
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.