How to Add a Border around an Array ?

Learn via video courses
Topics Covered

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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.


Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

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.


Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.


Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Code - 5 :

Output:

Explanation :
A border is added to the array elements with pad_width (2,3)(2,3) in linear_ramp mode with end_values (1,2)(-1,2).

  • 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 (2,3)(2,3), 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 (3,3)(3,3), 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.