Home > Enterprise >  How to pad an array A equal to the shape of array B
How to pad an array A equal to the shape of array B

Time:01-16

I have two arrays A and B

shape of array A = (1080,809)

shape of array B = (983,449)

I want to pad array B to make it as same as array A shape

How to pad in this condition or is there any formula to calculate the padding sizes?

CodePudding user response:

You can use numpy.pad. You need to craft a helper array as pad requires to feed the (pre, post) padding width per dimension:

extra = np.c_[(0,0), np.array(A.shape)-B.shape]
# array([[0, 2],
#        [0, 3]])

B2 = np.pad(B, extra, mode='constant')

input:

A = np.random.randint(2,4,size=(6, 6))
# array([[3, 2, 3, 2, 2, 3],
#        [3, 3, 3, 3, 3, 3],
#        [3, 2, 3, 2, 3, 2],
#        [2, 2, 3, 3, 3, 3],
#        [3, 2, 2, 2, 3, 2],
#        [3, 2, 3, 2, 3, 2]])

B = np.ones((4,3))

# array([[1, 1, 1],
#        [1, 1, 1],
#        [1, 1, 1],
#        [1, 1, 1]]))

output:

array([[1, 1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

If you want to specify the value to use as padding:

B2 = np.pad(B, extra, mode='constant', constant_values=(0, 9))
# array([[1, 1, 1, 9, 9, 9],
#        [1, 1, 1, 9, 9, 9],
#        [1, 1, 1, 9, 9, 9],
#        [1, 1, 1, 9, 9, 9],
#        [9, 9, 9, 9, 9, 9],
#        [9, 9, 9, 9, 9, 9]])

CodePudding user response:

Pre or Post padding using numpy.pad

For padding a 2D array with different padding widths over different axes, you can use numpy.pad it as below:

  1. The differences in number of elements between the 2 arrays over each axis will define how much you want to pad over those respective axes. Check how I define x, y below.

  2. The second parameter in np.pad() is pad_width. Here, [(0,5),(4,2)] simply means -> 0 padding before 5 padding after on axis 0; and 4 padding before 2 padding after on axis 1. Check how I use x,y for pre and post padding below.

import numpy as np

#dummy data
A = np.random.randint(1,2,(7,8))
B = np.random.randint(1,2,(3,5))

x, y = A.shape[0] - B.shape[0], A.shape[1] - B.shape[1]

#### POST PADDING ####
np.pad(B, [(0,x),(0,y)])

# array([[1, 1, 1, 1, 1, 0, 0, 0],
#        [1, 1, 1, 1, 1, 0, 0, 0],
#        [1, 1, 1, 1, 1, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0]])


#### PRE PADDING ####
np.pad(B, [(x,0),(y,0)])

# array([[0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 1, 1, 1, 1, 1],
#        [0, 0, 0, 1, 1, 1, 1, 1],
#        [0, 0, 0, 1, 1, 1, 1, 1]])

Do check the documentation linked above for more details on the various parameters you can customize your padding with.


Equal* pre-post padding using numpy.pad

For padding equally (almost) in pre and post over each axis, you can define a helper function similar to this one -

def padsize(i):
  if i%2==0:
    return i//2, i//2
  else:
    return i//2 1, i//2

padsize(21)
(11,10)

*Notice that if the difference is odd, it keeps one extra in the pre-padding and remaining in post-padding.

So, using this -

import numpy as np

#dummy data
A = np.random.randint(1,2,(7,8))
B = np.random.randint(1,2,(3,5))

def padsize(i):
  if i%2==0:
    return i//2, i//2
  else:
    return i//2 1, i//2

x, y = A.shape[0] - B.shape[0], A.shape[1] - B.shape[1]

(xx1, xx2), (yy1, yy2 ) = padsize(x), padsize(y) #(2, 2), (2, 1)

np.pad(B, [(xx1,xx2),(yy1,yy2)])
array([[0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0]])
  •  Tags:  
  • Related