Holdout method in python

Viewed 2889

How to do 6:4 holdout in python? I tried the following code:

X_train, X_test, y_train, y_test =  train_test_split(X,y, training_size=0.6, test_size=0.4)

But not sure whether it's right or not.

1 Answers

Refer to the documentation for train_test_split from scikit-learn.

To set both the size of the training and the test set you need to pass train_size (not training_size as in your code) and test_size.

To use 60% of your data for training and 40% for testing you can use this:

import numpy as np
from sklearn.model_selection import train_test_split

X = np.random.rand(100, 2)
y = range(100)

X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.6, test_size=0.4)

You can confirm that for the 100 datapoints used in this example you get a train set size of 60 and a test set size of 40:

print(len(X_train), len(X_test))
print(len(y_train), len(y_test))
> 60 40
> 60 40
Related