AppDividend
Latest Code Tutorials

np.pad: How to Use numpy pad() Function in Python

The np.pad() is a numpy library function used for padding the arrays. The padding is a process in which the array is appended with some values at the beginning and end.

Syntax

numpy.pad(array, pad_width, mode = 'constant', **kwargs)

Arguments

array: It is the array in which we want to perform padding.

pad_width: It is the combination beginning and ending size of the elements we want to pad to the array. ( 2, 4) Here 2 is the padding size that is needed to be added at the beginning, and 4 is the padding size that is needed to be added at the ending of the array. 

Mode: The mode in which the function should pad the values to the array. Some of the modes are. 

  • Constant – It is the default mode in which the values are padded. It pads with the constant value in the array. 
  • Edge – It pads with the edge value of the array. In the beginning, it pads with the 0th index element of the array. In the end, it pads with the last element of the array.
  • Linear_ramp – It fills the values with the end and edge values in a linear ramp manner.
  • Maximum – It pads with the maximum element from the list.
  • Mean – It pads with the mean value from the list.
  • Median – It pads with the median value from the list.
  • Minimum – It pads with the minimum element from the list.
  • Wrap – The start value is padded at last, and the end values are used to pad for the beginning. 
  • Empty – It pads with undefined values.
  • Reflect – It pads with the reflection of the array. 
  • Symmetric – It pads the reflection of the edge of the matrix.
  • constant_values –  The values are given in tuples like ( 2, 3 ) here, 2 is padded at the beginning of the array, and 3 is padded at the end of the array.
  • end_values – It is used along with the linear ramp to give the limit.
  • reflect_type – There are two types, odd or even.

Python program for padding constant values

# import numpy as np
import numpy as np

# creating an array and storing values
arr = [1, 4, 5, 6, 9]

# creating a padded array
res = np.pad(arr, (2, 4), "constant", constant_values=(10, 0))
print(res)

Output

[10 10 1 4 5 6 9 0 0 0 0]

In this program, we imported the numpy library. Then, we passed the array as arr. The padding size is 2, 4. The beginning padding is 2 and 3 in the ending. Mode is given as constant, and the constant values are 10 and 0.

Python program for padding maximum values

import numpy as np

# creating an array and storing values
arr = [1, 4, 5, 6, 9]

# creating a padded array
res = np.pad(arr, (2, 4), 'maximum')
print(res)

Output

[9 9 1 4 5 6 9 9 9 9 9]

In this program, the maximum value from the array will be used as the padding element.

Python program for padding linear_ramp

import numpy as np

# Creating an array and storing values
arr = [1, 4, 5, 6, 9]

# creating a padded array
res = np.pad(arr, (2, 4), 'linear_ramp', end_values=(2, 5))
print(res)

Output

[2 1 1 4 5 6 9 8 7 6 5]

Here, the “linear_ramp” argument is used along with the end_values.

That’s it for the np.pad() function in Python.

Related posts

np.apply_along_axis()

np.insert()

np.empty()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.