training_images = np.array([i for i in images if i not in validation_images])
The above is wrong (as noted in a comment below). What's a correct and faster way of doing this?
My validation_images is just
validation_images = images[::6]
and the shape of images is (60000, 784). This is a numpy array.
The current method is not acceptable because it is too slow.
Deleting element from NumPy array using np. The delete(array_name ) method will be used to do the same.
You can use the pop() method to remove an element from the array.
Now to remove the last element from the array create a Boolean array with length same as the array. All the elements in this Boolean array are True except for the last element. Then pass this Boolean array as index to the original NumPy Array. This will give an array with last element removed.
Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With