How to create TRAIN and TEST dataset using sklearn and Python

Hits: 115

How to create TRAIN and TEST dataset using sklearn and Python

When working with machine learning, it’s important to split the data into a training and testing set to evaluate the performance of your model. This is known as a train-test split, and it’s a common practice in machine learning.

In Python, the library scikit-learn provides an easy way to perform this split using the train_test_split function.

The first step is to import the library and load the dataset into a pandas dataframe. Then, the data is split into two sets: a training set and a testing set. The train_test_split function takes the dataset and splits it into the desired ratio, typically 80% for training and 20% for testing.

You can also pass the variable ‘random_state’ which make the split deterministic .

The function returns the dataframes for training and testing set, which can be used to train the model and evaluate its performance.

It’s worth noting that the ratio of data split may vary depending on the dataset size, you should use the split ratio that best suits your dataset.

In summary, train-test split is a crucial step in machine learning to evaluate the performance of your model. Using the train_test_split function in scikit-learn, it’s easy to split your data into a training and testing set in Python, making it a powerful tool for data scientists and machine learning practitioners.


In this Learn through Codes example, you will learn: How to create TRAIN and TEST dataset using sklearn and Python.


Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners