Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy array exclude some elements

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.

like image 342
ajfbiw.s Avatar asked Feb 09 '16 23:02

ajfbiw.s


People also ask

How do I get rid of one array those items that exist in another NumPy?

Deleting element from NumPy array using np. The delete(array_name ) method will be used to do the same.

How do I remove a specific element from an array in Python?

You can use the pop() method to remove an element from the array.

How do you remove the last element of a NumPy array in Python?

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.

How do I drop a column in NumPy array?

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.


1 Answers

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.

like image 122
MSeifert Avatar answered Sep 19 '22 12:09

MSeifert