Answers for "what is the use of zeros ways to numpy array in python"

11

declare numpy zeros matrix python

import numpy as np
dimensions = (3, 3)
np.zeros(dimensions) # 3x3 zeros matrix
Posted by: Guest on August-30-2020
3

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]])
Posted by: Guest on October-04-2020

Code answers related to "what is the use of zeros ways to numpy array in python"

Python Answers by Framework

Browse Popular Code Answers by Language