
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy pad() Function
The Numpy pad() function is used to pad an array with values along its edges. Padding is typically done to increase the size of an array by adding values (such as zeros or a constant) around its edges. The function provides flexibility in specifying the padding width, the mode of padding, and custom values.
Padding can be applied in various ways by setting the mode parameter to options such as constant, edge, symmetric, reflect, and more, depending on the desired behavior.
Syntax
Following is the syntax of the Numpy pad() function −
numpy.pad(array, pad_width, mode='constant', **kwargs)
Parameters
Following are the parameters of the Numpy pad() function −
- array - Input array to be padded.
- pad_width - Number of values padded to the edges of each axis. It can be an integer, tuple, or sequence of tuples.
- mode(optional) - Specifies the method of padding. Default is constant. Available modes include constant, edge, linear_ramp, reflect, symmetric, etc.
- kwargs(optional) - Additional keyword arguments depending on the padding mode, such as constant_values for constant padding.
Return Values
This function returns a padded array with the same or modified shape based on the specified padding.
Example
Following is a basic example to pad a numpy array using Numpy pad() function −
import numpy as np array = np.array([1, 2, 3]) padded_array = np.pad(array, pad_width=2, mode='constant') print("Padded Array:", padded_array)
Output
Following is the output of the above code −
Padded Array: [0 0 1 2 3 0 0]
Example: Padding with Different Modes
Using numpy.pad(), we can pad an array with different modes such as edge, reflect, or symmetric. In the following example, we have padded an array using the edge mode, which replicated on the edges −
import numpy as np array = np.array([1, 2, 3]) print("Original Array:",array) padded_array = np.pad(array, pad_width=2, mode='edge') print("Edge Padded Array:", padded_array)
Output
Following is the output of the above code −
Original Array: [1 2 3] Edge Padded Array: [1 1 1 2 3 3 3]
Example: Padding Multi-dimensional Arrays
The numpy.pad() function can also pad multi-dimensional arrays along specified axes with different padding widths. Here, a 2D array is padded as follows −
- (1, 1): specifies padding for the rows, where 1 row is added above and 1 row below the original array, filled with 0s.
- (2,2): specifies padding for the columns, where 2 columns are added to the left and 2 columns to the right of the original array, also filled with 0s.
import numpy as np array = np.array([[1, 2], [3, 4]]) padded_array = np.pad(array, pad_width=((1, 1), (2, 2)), mode='constant', constant_values=0) print("2D Padded Array:\n", padded_array)
Output
Following is the output of the above code −
2D Padded Array: [[0 0 0 0 0 0] [0 0 1 2 0 0] [0 0 3 4 0 0] [0 0 0 0 0 0]]
Example: Custom Padding with Constant Values
Using numpy.pad(), we can customize the padding values in constant mode by using the constant_values parameter. Here, a 1D array is padded with the constant value 9 on both sides, repeated 3 times −
import numpy as np array = np.array([5, 6, 7]) padded_array = np.pad(array, pad_width=3, mode='constant', constant_values=9) print("Constant Padded Array:", padded_array)
Output
Following is the output of the above code −
Constant Padded Array: [9 9 5 6 7 9 9]