How to Add a Border around an Array ?
For adding a border around a Numpy array, the Python library provides a NumPy module function called numpy.pad() with many required and optional parameters. Adding a border around a NumPy array means padding the array or inserting the array into a boundary of some integers. The dimensions of the array are changed due to padding or putting the array inside, adding a border.
Syntax
- Pad_width :
It defined the beginning and ending size of the border. - Mode :
Borders can be added in various ways, such as mode, median, and so on, and are always constant by default.
Let's understand the implementation of this function with the help of the following examples.
Adding a Border Around a 1-D Array
Code - 1 :
Output :
Explanation:
An array arr is created using thenp.arange function. The border of constant value 0 is added to the array using the np.pad function.
Code - 2 :
Output:
Explanation :
If we define constant value as 1 and pad_width as 2, then 1 is added twice as a border at the beginning and end of the array using constant mode.
Code - 3 :
Output :
Explanation:
An array, arr, is created using the np.arange function. Adding a border of width (2,3) with value (9,2), i.e. 2 numbers at the start of value 9 and 3 numbers at the end of the array of values (2,1) using the np.pad function.
Code - 4 :
Output:
Explanation:
An array, arr, is created using the np.arange function. By using the np.pad function, a border is added around the array using edge mode. Edge elements are expanded to the pad_width given, i.e., the starting element at the edge is 1, which is added 2 times at the start, and the edge element at the end is 17, which is added 3 times.
Code - 5 :
Output:
Explanation :
A border is added to the array elements with pad_width in linear_ramp mode with end_values .
- In linear_ramp, values are added in such a way that they do not exceed the provided limits, in this example, the starting limit is -1, so two numbers, -1 and 0, are added at the start, and three numbers, in decreasing order from the edges up to 2, are added at the end.
- end_values are the limits.
Code - 6 :
Output :
Explanation :
The maximum element of the array is added as a border around the array with width , i.e., 2 numbers at the start and 3 numbers at the end of the array.
There are many more modes, like:
- Minimum:
padding is done using the minimum value of the array. - median :
The median of the array is used for padding. - mean :
It pads the array with the mean of the array. - reflect :
It pads with the reflection of the array. - symmetric :
The reflection of the edges of the array is used for padding. - wrap :
wraps the text within the body so that the last values of the array are at the beginning and the first values are at the end. - empty :
Undefined values are padded as the border around the array.
Adding a Border Around a 2-D Array
Code :
Output :
Explanation :
A two-dimensional array of shapes , i.e., three rows and three columns with zero values, is created using the np.zeros function. Using the np.pad function, the border of constant value 1 of pad_with 1 is added around the array.
Conclusion
- A border around a NumPy array can be added by using the np.pad function provided by the NumPy module.
- np.pad function have various required and optional parameters for adding a border in the desired manner.
- There are numerous border modes available, such as mean, maximum, edge, and so on.