Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy remove a dimension from np array

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) 
like image 894
Kevin Avatar asked May 11 '16 02:05

Kevin


People also ask

How do I remove a specific value from a NumPy array?

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.

How do I drop an axis in NumPy?

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.).

How do I remove an element from Ndarray?

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.

How do you remove the size of a list in Python?

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.

How to strip out the last dimension from a NumPy array?

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 based on index?

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.

How do I delete a column from a 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.

How do I add a dimension to a NumPy array?

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.


1 Answers

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.

like image 179
Matt Messersmith Avatar answered Sep 21 '22 19:09

Matt Messersmith