Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split dataset to train, test and valid in Python? [duplicate]

I have a dataset like this

my_data= [['Manchester', '23', '80', 'CM',
  'Manchester', '22', '79', 'RM',
  'Manchester', '19', '76', 'LB'],
 ['Benfica', '26', '77', 'CF',
  'Benfica', '22', '74', 'CDM',
  'Benfica', '17', '70', 'RB'],
 ['Dortmund', '24', '75', 'CM',
  'Dortmund', '18', '74', 'AM',
  'Dortmund', '16', '69', 'LM']
]

I know that using train_test_split from sklearn.cross_validation, and I've tried with this

from sklearn.model_selection import train_test_split
train, test = train_test_split(my_data, test_size = 0.2)

The result just split into test and train. I wish to divide it to 3 separate sets with randomized data.

Expected: Test, Train, Valid

like image 514
dede.brahma Avatar asked Sep 22 '20 06:09

dede.brahma


2 Answers

You can simply use train_test split twice

X_train, X_test, y_train, y_test 
    = train_test_split(X, y, test_size=0.2, random_state=1)

 X_train, X_val, y_train, y_val 
    = train_test_split(X_train, y_train, test_size=0.25, random_state=1)

also, the answer can be found here

like image 145
Rafał Prońko Avatar answered Oct 13 '22 04:10

Rafał Prońko


It can be achieved using numpy+pandas, see script below splitting 0.6 + 0.2 + 0.2:

train_size = 0.6
validate_size = 0.2
train, validate, test = np.split(my_data.sample(frac=1), [int(train_size * len(my_data)), int((validate_size + train_size) * len(my_data))])
like image 22
Raymond Reddington Avatar answered Oct 13 '22 05:10

Raymond Reddington