Myntra Interview Question | How to split a dataset into train and test in python
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 2 )
# First import the necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
# Load the Dataset
data = pd.read_csv(‘filename.csv’)
# create target variable and features – suppose your target variable is Rent
y = data.Rent
x = data.drop(‘Rent’,axis=1)
# create the train and test split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.3, random_state=42)
# The ordering of variables is important in the above line of code
# test_size will keep 30% data in the test set
# random_state will help in keeping the same samples in the train and test set.
Sorry it a private answer.