# How to split your dataset into train, test, and validation sets?

## Introduction

If you’ve been using the `train_test_split` method by `sklearn` to create the train, test, and validation datasets, then I know your pain.

> ![](https://miro.medium.com/v2/resize:fit:700/1*NQaN71ejH_eTUxhRLwiJcA.png align="left")
> 
> Splitting datasets into the test, train, and validation datasets

While `sklearn` certainly provides us with a way to achieve our objective, however, it is a long-drawn-out procedure as we have to repeat the process twice adjusting the split ratio with every step.

**But rejoice,** `fast_ml` **is here!**

It offers a straightforward and to-the-point method to achieve the three different datasets with a single line of code.

It is the `train_valid_test_split` method!

It not only splits the data as we require but also separates the dependent variable `y` from the independent variables `X` in the same line of code.

## Code walkthrough

Let’s check out how it’s done ([notebook](https://github.com/utkarshkant/25-short-code-snippets_Python))!

***Step 1:*** Download the `fast_ml` library and Import the necessary packages and methods

> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682426224027/00c5633e-53c8-4594-b459-8b5c51602e45.png align="center")

***Step 2:*** Load the dataset into a pandas data frame.

> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682426872886/796064bf-fb72-4eb2-96e4-f79b988dfd5f.png align="center")

***Step 3:*** Split the dataset

Once the data is loaded and ready to split, simply call the `train_valid_test_split` method and pass the dataset with the supporting parameters as below.

> ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682426970820/a485c4a9-65d7-4d09-922f-020cdc5f6cf7.png align="center")

The datasets have been successfully split into train, test, and validation datasets. 🎉

> **💡 NOTE**  
> The split datasets retain their original index and resetting it is an optional step.

You can now proceed with your modeling.

## Conclusion

Thanks to the team at `fast_ml`, the long-drawn-out task of splitting our dataset into independent and dependent features and then into training, testing, and validation datasets has been condensed into a single line of code. ⚡

You can find this notebook here:

%[https://github.com/utkarshkant/25-short-code-snippets_Python/blob/master/train_valid_test_split.ipynb] 

Let me know how you liked this quick article in the comments below, and feed free to reach out!
