How to use pad, padding process (Python-numpy)



Python
library
pip

MeCab

numpy
digitize

mgrid

pad

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

scipy
interpolate
tkinter
postscript

image display

frame, grid

other
linear interpolation

OpenAI gym
CartPole-v0


Release date:2024/1/13         

In Japanese


■Description of the pad function

Pad is a function that adds numerical values to the edge of an array, and is called padding processing. append is also a way to add numbers, but pad may be more convenient.

■Specific example using pad function

The following information is required when using numpy.

import numpy as np


<For one-dimensional array>

a = np.array([1,2,3,])

b = np.pad(a,[2,3]) # Add two 0s before the array and three 0s after it
print(b)

    → array([0, 0, 1, 2, 3, 0, 0, 0])

b = np.pad(a,[2,3] , constant_values=(5,6)) # Add two 5s before the array and three 6s after it
print(b)

    → array([5, 5, 1, 2, 3, 6, 6, 6])

b = np.pad(a,[2,3],'edge') #Add the same number as the edge
print(b)

    → array([1, 1, 1, 2, 3, 3, 3, 3])


<For a 2-dimensional array>

a = np.array([[1,2,3],[4,5,6]])

b = np.pad(a , (1,2)) #Add one zero to the top and front of the array, and two zeros to the bottom and left of the array.
print(b)

    → array([[0, 0, 0, 0, 0, 0],
                [0, 1, 2, 3, 0, 0],
                [0, 4, 5, 6, 0, 0],
                [0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0]])


b = np.pad(a , [(0,1),(2,3)]) #Add one 0 to the bottom of the array, two 0s to the front (left), and three 0s to the back (right).
print(b)

    →array([[0, 0, 1, 2, 3, 0, 0, 0],
                [0, 0, 4, 5, 6, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0]])


<For n-dimensional array ①>

a=np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
print(a)

    → array([[[1, 2],
                [3, 4]],

                [[5, 6],
                [7, 8]]])


b = np.pad(a , [(1,1),(2,3),(1,2)]) #The front and back of the array are (1,1), the top and bottom are (2,3), and the front and back are (1,2)
print(b)

    → 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],

                [[0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 1, 2, 0, 0],
                [0, 3, 4, 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, 5, 6, 0, 0],
                [0, 7, 8, 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, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0]]])


<For n-dimensional array ②>

a=np.array([[[1,2],[3,4]],[[5,6],[7,8]]]
a=a.reshape(2,2,2,1)
print(a)

    → array([[[[1],
                [2]],

                [[3],
                [4]]],


                [[[5],
                [6]],

                [[7],
                [8]]]])


b = np.pad(a , [(1,0),(0,1),(1,1),(2,3)])
print(b)

    → array([[[[0, 0, 0, 0, 0, 0], #1 part of (1,0) for all three arrays
                [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, 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, 0, 0], #Top and bottom are (1,1), front and back are (2,3)
                [0, 0, 1, 0, 0, 0],
                [0, 0, 2, 0, 0, 0],
                [0, 0, 0, 0, 0, 0]],

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

                [[0, 0, 0, 0, 0, 0], #1 part in (0,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, 5, 0, 0, 0],
                [0, 0, 6, 0, 0, 0],
                [0, 0, 0, 0, 0, 0]],

                [[0, 0, 0, 0, 0, 0],
                [0, 0, 7, 0, 0, 0],
                [0, 0, 8, 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]]]])









List of related articles



Python
library
pip

MeCab

numpy
digitize

mgrid

pad

polyfit

prod

shape

matplotlib
figure

pcolormesh

scatter

pytorch
BCELoss, MSELoss

device

Embedding

TensorDataset, Dataloader

RNN, LSTM
scikit-learn
SVC

scipy
interpolate
tkinter
postscript

image display

frame, grid

other
linear interpolation

OpenAI gym
CartPole-v0