Numpy array exclude some elements
I'm always using boolean masks for such things, you could consider:
# Mask every sixth row
mask = (np.arange(images.shape[0]) % 6) != 0
# Only use the not masked images
training_images = images[mask]
The validation set would then be every masked element:
validation_images = images[~mask]
Mathematical operations on numpy arrays work element wise, so taking the modulo (%
) will be executed on each element and returns another array with the same shape. The != 0
works also element-wise and compares if the modulo is not zero. So the mask is just an array containing False
where the value is not an int * 6
and True
where it is.