I have some images I want to work with, the problem is that there are two kinds of images both are 106 x 106 pixels, some are in color and some are black and white.
one with only two (2) dimensions:
(106,106)
and one with three (3)
(106,106,3)
Is there a way I can strip this last dimension?
I tried np.delete, but it did not seem to work.
np.shape(np.delete(Xtrain[0], [2] , 2)) Out[67]: (106, 106, 2)
To remove an element from a NumPy array: Specify the index of the element to remove. Call the numpy. delete() function on the array for the given index.
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.).
To delete multiple elements from a numpy array by index positions, pass the numpy array and list of index positions to be deleted to np. delete() i.e. It deleted the elements at index position 1,2 and 3 from the numpy array. It returned a copy of the passed array by deleting multiple element at given indices.
Singular dimensions (value 1) can be removed with indexing, [0] , or squeeze . reshape also removes demensions (or adds them), but you have to pay attention to the total number of elements. If the old shape had 12 elements, the new has to have 12 as well.
You could use numpy's fancy indexing (an extension to Python's built-in slice notation): A shape of (106, 106, 3) means you have 3 sets of things that have shape (106, 106). So in order to "strip" the last dimension, you just have to pick one of these (that's what the fancy indexing does).
How to remove elements from a numpy array? - Data Science Parichay How to remove elements from a numpy array? In this tutorial, we will look at how to remove elements from a numpy array based on their index with the help of simple examples. You can use the np.delete () function to remove specific elements from a numpy array based on their index.
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.
Use numpy.reshape () to convert to any shape, and numpy.newaxis, numpy.expand_dims () to add a new dimension of size 1. See the following article for details. Specifying numpy.ndarray as the first argument of numpy.squeeze () returns numpy.ndarray with all dimensions of size 1 removed.
You could use numpy's fancy indexing (an extension to Python's built-in slice notation):
x = np.zeros( (106, 106, 3) ) result = x[:, :, 0] print(result.shape)
prints
(106, 106)
A shape of (106, 106, 3)
means you have 3 sets of things that have shape (106, 106)
. So in order to "strip" the last dimension, you just have to pick one of these (that's what the fancy indexing does).
You can keep any slice you want. I arbitrarily choose to keep the 0th, since you didn't specify what you wanted. So, result = x[:, :, 1]
and result = x[:, :, 2]
would give the desired shape as well: it all just depends on which slice you need to keep.
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