Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a padded slice of a multidimensional array?

Tags:

python

numpy

I am stuck on a little issue in the project I am currently working on.

Getting straight to the point, let's assume I have a 2-dimensional numpy.array - I will call it arr.

I need to slice arr, but this slice must contain some padding depending on the selected interval.

Example:

arr = numpy.array([
    [  1,  2,  3,  4,  5],
    [  6,  7,  8,  9, 10],
    [ 11, 12, 13, 14, 15],
    [ 16, 17, 18, 19, 20],
    [ 21, 22, 23, 24, 25]
])

Actually, numpy's response for arr[3:7, 3:7] is:

array([[19, 20],
       [24, 25]])

But I need it to be padded as if arr were bigger than it really is.

Here is what I need as response for arr[3:7, 3:7]:

array([[19, 20,  0,  0],
       [24, 25,  0,  0],
       [ 0,  0,  0,  0],
       [ 0,  0,  0,  0]])

This padding should also occur in case of negative indices. If the requested slice is bigger than the whole image, padding must occur in all sides, if needed.

Another example, negative indices. This is the expected result for arr[-2:2, -1:3]:

array([[ 0,  0,  0,  0],
       [ 0,  0,  1,  2],
       [ 0,  0,  6,  7],
       [ 0,  0, 11, 12]])

Is there any native numpy function for this? If not, any idea of how can I implement this?

like image 541
Rodrigo Siqueira Avatar asked Apr 30 '15 08:04

Rodrigo Siqueira


People also ask

How do you add padding to an array in Python?

pad() function is used to pad the Numpy arrays. Sometimes there is a need to perform padding in Numpy arrays, then numPy. pad() function is used. The function returns the padded array of rank equal to the given array and the shape will increase according to pad_width.

What is padding an array?

The array padding transformation sets a dimension in an array to a new size. The goal of this transformation is to reduce the number of memory system conflicts. The transformation is applied to a full function AST. The new size can be specified by the user or can be computed automatically.

How do you slice a 2d NP array?

Slice Two-dimensional Numpy Arrays To slice elements from two-dimensional arrays, you need to specify both a row index and a column index as [row_index, column_index] . For example, you can use the index [1,2] to query the element at the second row, third column in precip_2002_2013 .

Can you slice Numpy arrays?

You can slice a numpy array is a similar way to slicing a list - except you can do it in more than one dimension.


2 Answers

About the first part of your question you can use a simple indexing, and you can create a zero_like of your array with numpy.zeros_like then assign the special part :

>>> new=numpy.zeros_like(arr)
>>> part=arr[3:7, 3:7]
>>> i,j=part.shape
>>> new[:i,:j]=part
>>> new
array([[19, 20,  0,  0,  0],
       [24, 25,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0]])

But for the second case you can not use a negative indexing for for numpy arrays like this.Negative indices are interpreted as counting from the end of the array so if you are counting from -2 actually in a 5x5 array there are not any row between -2 and 2 so the result would be an empty array :

>>> arr[-2:2]
array([], shape=(0, 5), dtype=int64)
like image 97
Mazdak Avatar answered Oct 23 '22 05:10

Mazdak


You can do something like:

print np.lib.pad(arr[3:7,3:7], ((0, 2), (0, 2)), 'constant', constant_values=(0,0 ))



[[19 20  0  0]
 [24 25  0  0]
 [ 0  0  0  0]
 [ 0  0  0  0]]

For the negative indexing:

print np.lib.pad(arr[ max(0,-1):3 , 0:2 ], ((1, 0), (2, 0)), 'constant', constant_values=(0,0 ))

[[ 0  0  0  0]
 [ 0  0  1  2]
 [ 0  0  6  7]
 [ 0  0 11 12]]

Check here for reference

like image 45
George Avatar answered Oct 23 '22 06:10

George