NumPy pad extends array boundaries using explicit widths and a chosen filling rule
Adding boundary entries changes an array's size and requires a rule for what those new positions contain. A zero border and a copied edge are different choices, even when they produce the same shape. Academia Master Trader explains NumPy pad in English for readers in North Macedonia. We will extend a short sequence and a small matrix, naming the widths and mode each time. Padding is an array construction rule, not a source of new measurements or evidence of financial performance.
Separate the input from its new boundary
After import numpy as np, define x = np.array([8,3,6]). The call np.pad(x,(1,2),mode='constant') returns [0,8,3,6,0,0]. The width pair means one position before the original sequence and two after it. Constant mode uses zero by default. The original entries remain together, in their original order, between the added boundaries.
The input shape is (3,), and the output shape is (6,). Its length is three original positions plus one before plus two after. Eight moves from original index zero to output index one because a new leading position was added. Three and six move by the same offset. Their relative order and numerical values remain unchanged.
Give constant padding an explicit value
The call np.pad(x,(1,2),mode='constant',constant_values=5) returns [5,8,3,6,5,5]. The widths have not changed, so the output length remains six. Only the filling rule's constant has changed from the default zero to five. The added five at the start is not an original observation, and neither are the two added fives at the end.
A padding width is a count of positions, whereas a constant value supplies their contents. Confusing those roles would produce a different array. Write the width pair and the constant separately when explaining a result. A reader should be able to identify which entries came from the input and which were introduced by the chosen boundary rule.

Read the figure from the unchanged interior outward
The figure highlights the original sequence in gold inside both padded outputs. Begin with eight, three and six, then look at the single cell before them and the two cells after them. In the constant example those cells contain zeros. In the edge example they contain copies of the nearest boundary entries. The source block stays identical in both cases.
The figure's extra cells represent added array positions, not additional collected evidence. Keeping their colour separate from the original interior makes that distinction visible. Both examples have six entries, so their shapes alone cannot identify the selected mode. Inspect the boundary contents as well as the width counts before deciding what construction was used.
Use edge mode to extend the endpoint values
The call np.pad(x,(1,2),mode='edge') returns [8,8,3,6,6,6]. The leading added position copies the first entry eight. The two trailing added positions copy the last entry six. Original three remains between original eight and six. Edge mode does not repeat the whole three entry pattern, and it does not reverse the sequence.
The same widths therefore produce different outputs under constant and edge modes. Choosing a boundary rule is a modelling or processing decision, not something the input values alone determine. State it explicitly. A copied endpoint is still a copy, and a chosen constant is still a chosen constant; neither should be presented as a newly observed value outside the supplied sequence.
Specify widths for every matrix axis
Define a = np.array([[8,3],[2,9]]), with shape (2,2). The call np.pad(a,((1,0),(0,1)),mode='constant') returns [[0,0,0],[8,3,0],[2,9,0]], with shape (3,3). The first width pair adds one row before and none after. The second pair adds no column before and one after. All new positions contain the default constant zero.
The original eight is now at (1,0), while original nine is at (2,1). Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, a before and after width for each axis, and the selected mode. Then mark the original interior and verify the new boundary separately. Those checks explain the larger array without confusing added positions with new information.








