I don't know why numpy pad works like this

I made multidimensional array.

And I pad zero to this array.

And I got this array.

%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%

I’m sorry there are lots of pictures.
I don’t understand why I can have four two-dimensional arrays with all zero elements before and after the original array. (I mean the array in the first picture and the array in the last picture.)
Someone please tell me how numpy pad works on multidimensional array.

Might help to start simple and build up.

``````a = np.random.randint(1,5,(2))
print(a.shape)
print(a)
``````

(2,)
[1 1]

``````a_pad = np.pad(a,(1,1),mode='constant',constant_values=(0))
print(a_pad.shape)
print(a_pad)
``````

(4,)
[0 1 1 0]

Above we padded with the constant 0 both before and after in the single dimension. Next, let’s do 2 dimensions…

``````a = np.random.randint(1,5,(2,2))
print(a.shape)
print(a)
``````

(2, 2)

[[3 2]
[4 4]]

``````a_pad = np.pad(a,(1,1),mode='constant',constant_values=(0))
print(a_pad.shape)
print(a_pad)
``````

(4, 4)

[[0 0 0 0]
[0 3 2 0]
[0 4 4 0]
[0 0 0 0]]

Again we padded with the constant 0 both before and after, but now in in all dimensions, which in this case is 2. Let’s try again with 3 dimensions…

``````a = np.random.randint(1,5,(2,2,2))
print(a.shape)
print(a)
``````

(2, 2, 2)
[[[2 2]
[1 1]]

[[4 2]
[1 3]]]

``````a_pad = np.pad(a,(1,1),mode='constant',constant_values=(0,0))
print(a_pad.shape)
print(a_pad)
``````

(4, 4, 4)

[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]

[[0 0 0 0]
[0 2 2 0]
[0 1 1 0]
[0 0 0 0]]

[[0 0 0 0]
[0 4 2 0]
[0 1 3 0]
[0 0 0 0]]

[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]

Once again, we padded with the constant 0 both before and after in all dimensions, which in this case is 3.

The pattern is the same for all multidimensional inputs because of the way the `pad_width` parameter is being specified. With only a single value for the before_i and a single value for the after_i, it will be applied uniformly across all dimensions. To control the multi-dimensions separately, you have to use a more complicated expression for `pad_width`. For example, what if we only wanted to pad one of the dimensions, and leave the rest unpadded?

``````a = np.random.randint(1,5,(2,2,2,2))
print(a)
``````

[[[[1 4]
[3 3]]

[[3 3]
[3 4]]]

[[[1 3]
[4 1]]

[[4 1]
[1 3]]]]

``````a_pad = np.pad(a,((0,0),(0,0),(0,0),(1,1)),mode='constant',constant_values=(0))
print(a_pad)
``````

[[[[0 1 4 0]
[0 3 3 0]]

[[0 3 3 0]
[0 3 4 0]]]

[[[0 1 3 0]
[0 4 1 0]]

[[0 4 1 0]
[0 1 3 0]]]]

https://numpy.org/doc/stable/reference/generated/numpy.pad.html

3 Likes

Thanks for your reply!!! Now I understand how np.pad works because of your help!!! Thank you so much!!!

1 Like