Rachael E
Rachael E

Reputation: 41

Modifying train_test_split function

Can I split a data set into training and test sets based on index value (every 10th row as train data and the remaining as test data) using train_test_split() rather than passing test_size and random_state arguments?

Upvotes: 3

Views: 363

Answers (1)

H.Bukhari
H.Bukhari

Reputation: 2291

ok try this, you can use ::n, it will return every nth that you will specify, here is the example:

df=pd.DataFrame({'number': np.arange(100), })

if we want to get values every 10th :

print(df[::10])

result:

    number
0        0
10      10
20      20
30      30
40      40
50      50
60      60
70      70
80      80
90      90

you can do same thing with numpy array:

np.arange(100)

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

every 9th value:

np.arange(100)[::9]

output:

array([ 0,  9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99])

edit:

def getting_train_val(dataframe, interval=10):
    x_valid = dataframe[::interval]
    x_test = dataframe[~ dataframe(dataframe[::interval])].dropna()
    return x_valid, x_test

Upvotes: 2

Related Questions