python array of zeros code example

Example 1: declare numpy zeros matrix python

import numpy as np
dimensions = (3, 3)
np.zeros(dimensions) # 3x3 zeros matrix

Example 2: python how to pad a numpy array with zeros

# Basic syntax:
# For 1D array:
np.pad(numpy_array, (pad_left, pad_right), 'constant')

# For 2D array:
np.pad(numpy_array, ((pad_top, pad_bottom), (pad_left, pad_right)), 'constant')

# Example usage:
import numpy as np
numpy_array = np.array([1,2,3,4,5]) # 1D array
np.pad(numpy_array, (2, 3), 'constant') # Pad 2 left, 3 right
--> array([0, 0, 1, 2, 3, 4, 5, 0, 0, 0])

numpy_array = np.array([[1,2],[3,4]]) # 2D array
array([[1, 2], # Before padding
       [3, 4]])
np.pad(numpy_array, ((1,2),(2,1)), 'constant') # Pad 1 top, 2 bottom, 2 left, 1 right
array([[0, 0, 0, 0, 0], # After padding
       [0, 0, 1, 2, 0],
       [0, 0, 3, 4, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

Example 3: np.zeros([8,8,3])

>>> np.zeros([8,8,3])

array([[ 0.],
       [ 0.]])