Numpy array excludes some elements

training_images = np.array([i for i in images if i not in validation_images]) 

The foregoing is incorrect (as noted in the comment below). What is the right and quick way to do this?

My validation_images files are just

  validation_images = images[::6] 

and the form of images (60000, 784). This is a numpy array.

The current method is not acceptable because it is too slow.

+5
source share
1 answer

I always use logical masks for such things, you might think:

 # Mask every sixth row mask = (np.arange(images.shape[0]) % 6) != 0 # Only use the not masked images training_images = images[mask] 

Then each masked element will be a check set:

 validation_images = images[~mask] 

Mathematical operations on the element of work of the numpy array are wise, therefore, for each element modulo ( % ) another array with the same shape will be executed. != 0 also works on elements and compares if the module is not equal to zero. Thus, the mask is an array containing False , where the value is not int * 6 and True , where it is.

+4
source

Source: https://habr.com/ru/post/1242663/


All Articles