Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add a border around a NumPy array?
Adding a border around a NumPy array is useful in image processing, data visualization, and scientific computing. NumPy provides several methods to add borders: zero padding, constant padding, and concatenation.
In this article, we will explore three different techniques to add borders around NumPy arrays with practical examples.
Method 1: Zero Padding with np.pad()
The simplest method uses np.pad() with mode='constant' (default padding value is 0) ?
import numpy as np
# Create a sample 2D array
original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Add border of width 1 with zeros
bordered_array = np.pad(original_array, pad_width=1, mode='constant')
print("Original Array:")
print(original_array)
print("\nWith Zero Border:")
print(bordered_array)
Original Array: [[1 2 3] [4 5 6] [7 8 9]] With Zero Border: [[0 0 0 0 0] [0 1 2 3 0] [0 4 5 6 0] [0 7 8 9 0] [0 0 0 0 0]]
Method 2: Constant Value Padding
Use constant_values parameter to specify a custom border value ?
import numpy as np
original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Add border with constant value 99
bordered_array = np.pad(original_array, pad_width=2, mode='constant', constant_values=99)
print("Original Array:")
print(original_array)
print("\nWith Constant Border (99):")
print(bordered_array)
Original Array: [[1 2 3] [4 5 6] [7 8 9]] With Constant Border (99): [[99 99 99 99 99 99 99] [99 99 99 99 99 99 99] [99 99 1 2 3 99 99] [99 99 4 5 6 99 99] [99 99 7 8 9 99 99] [99 99 99 99 99 99 99] [99 99 99 99 99 99 99]]
Method 3: Manual Concatenation
Create border arrays manually and concatenate them with the original array ?
import numpy as np
def add_border_concatenation(arr, border_width, border_value=0):
height, width = arr.shape
# Create border arrays
top_border = np.full((border_width, width), border_value)
bottom_border = np.full((border_width, width), border_value)
# Add top and bottom borders
temp_array = np.concatenate([top_border, arr, bottom_border], axis=0)
# Create left and right borders
new_height = temp_array.shape[0]
left_border = np.full((new_height, border_width), border_value)
right_border = np.full((new_height, border_width), border_value)
# Add left and right borders
final_array = np.concatenate([left_border, temp_array, right_border], axis=1)
return final_array
original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
bordered_array = add_border_concatenation(original_array, border_width=1, border_value=5)
print("Original Array:")
print(original_array)
print("\nWith Concatenation Border:")
print(bordered_array)
Original Array: [[1 2 3] [4 5 6] [7 8 9]] With Concatenation Border: [[5 5 5 5 5] [5 1 2 3 5] [5 4 5 6 5] [5 7 8 9 5] [5 5 5 5 5]]
Advanced Padding Options
NumPy's pad() function offers several padding modes for different use cases ?
import numpy as np
original_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Reflect padding (mirror values)
reflect_border = np.pad(original_array, pad_width=1, mode='reflect')
# Edge padding (repeat edge values)
edge_border = np.pad(original_array, pad_width=1, mode='edge')
print("Reflect Padding:")
print(reflect_border)
print("\nEdge Padding:")
print(edge_border)
Reflect Padding: [[5 4 5 6 5] [2 1 2 3 2] [5 4 5 6 5] [8 7 8 9 8] [5 4 5 6 5]] Edge Padding: [[1 1 2 3 3] [1 1 2 3 3] [4 4 5 6 6] [7 7 8 9 9] [7 7 8 9 9]]
Comparison
| Method | Performance | Flexibility | Best For |
|---|---|---|---|
np.pad() |
Fast | High | Most use cases |
| Constant padding | Fast | Medium | Custom border values |
| Concatenation | Slower | High | Complex border patterns |
Conclusion
Use np.pad() for most border-adding tasks as it's efficient and flexible. Choose concatenation when you need complex custom border patterns that np.pad() cannot achieve.
