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