Reputation: 4093
I have a numpy array a. I would like to select a random sample of this array as a test and training set for cross-validation. As a training set, I use slicing by selecting the entries idx. Is there a way to select a compliment of these entries? i.e. all entries that are NOT in idx.
# N: size of numpy array a.
idx = random.sample(np.arange(N),N/10) # select random sample
train(a[idx]) # train on this random sample
test(a[ NOT idx]) # test on the rest.
How to call the rest of the entries in a compact way for the last line? Thanks.
Upvotes: 1
Views: 89
Reputation: 879331
If you make idx
a boolean array, then you can select the complement with ~idx
:
import numpy as np
N = len(a)
idx = np.zeros(N, dtype='bool')
idx[np.random.choice(np.arange(N), size=N/10, replace=False)] = True
train(a[idx]) # train on this random sample
test(a[~idx]) # test on the rest.
Upvotes: 3