 # padding elements of a numpy array

• Last Update :
• Techknowledgy :

Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.,Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.,Pads with the reflection of the vector mirrored along the edge of the array.,Pads with the mean value of all or part of the vector along each axis.

`padding_func(vector, iaxis_pad_width, iaxis, kwargs)`
```>>> a = [1, 2, 3, 4, 5] >>>
np.pad(a, (2, 3), 'constant', constant_values = (4, 6))
array([4, 4, 1, ..., 6, 6, 6])```
```>>> np.pad(a, (2, 3), 'edge')
array([1, 1, 1, ..., 5, 5, 5])```
```>>> np.pad(a, (2, 3), 'linear_ramp', end_values = (5, -4))
array([5, 3, 1, 2, 3, 4, 5, 2, -1, -4])```
```>>> np.pad(a, (2, ), 'maximum')
array([5, 5, 1, 2, 3, 4, 5, 5, 5])```
```>>> np.pad(a, (2, ), 'mean')
array([3, 3, 1, 2, 3, 4, 5, 3, 3])```

Suggestion : 2

Last Updated : 01 Oct, 2020

Output:

`[6 6 6 1 3 2 5 4 4 4]`

Suggestion : 3

June 6, 2022May 23, 2022

Before diving into how to use the `np.pad()` function, it can be helpful to understand the basic syntax of the function. Let’s take a look at what the function looks like and then explore the different parameters the function has to offer:

```# Understanding the np.pad() Function
array,
mode = 'constant', **
kwargs
)```

In this first example, you will learn how to pad an array using the NumPy pad function. Let’s start by looking at an array of one dimension, to better understand what is happening.

```# Using the np.pad() Function with a Single Dimension
import numpy as np

arr = np.array([1, 2, 3])

# Returns:
#[0 0 1 2 3 0 0]```

In the example above, we padded an array with a width of 2. By default, NumPy will use the value of 0. We can customize the value by changing the `constant_value=` parameter. Let’s use the value of 100 to pad our array:

```# Padding an Array with a Non - Default Value
import numpy as np

arr = np.array([1, 2, 3])

# Returns:
#[100 100 1 2 3 100 100]```

Padding an array with the `np.pad()` function can also be done in varied amounts of padding. This can be helpful if you want to apply padding, say, only to one or two sides. We can do this by passing in a sequence into the `pad_width=` parameter. Let’s take a look at what this looks like:

```# Padding Arrays with Different Amounts
import numpy as np

arr = np.arange(9).reshape(3, 3)

# Returns:
#[[0 0 0 0] #[0 1 2 0] #[3 4 5 0] #[6 7 8 0]]```

This is better demonstrated by using an example:

```# Padding an Array with the Nearest Value Using np.pad()
import numpy as np

arr = np.arange(9).reshape(3, 3)

# Returns:
#[[0 0 1 2 2] #[0 0 1 2 2] #[3 3 4 5 5] #[6 6 7 8 8] #[6 6 7 8 8]]```

Suggestion : 4

The syntax of the `NumPy.pad()` function is as follows.

`numpy.pad(array, pad_width, mode = 'constant', ** kwargs)`

The following code uses the `NumPy.pad()` function to pad a `NumPy` array in Python.

```import numpy as np
x = np.array([
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]
])
y = np.pad(x, [(0, 1), (0, 1)], mode = 'constant')
print(y)```

The above code provides the following output.

```[
[1. 1. 1. 1. 0.]
[1. 1. 1. 1. 0.]
[1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]
]```

Suggestion : 5

The numpy module of Python provides a function called numpy.pad() to perform padding in the array. This function has several required and optional parameters.,This function returns the padded array of rank equal to the array, whose shape increase according to pad_width.,This value pads the array via vector reflection, which is mirrored on the starting and ending vector values, along each axis.,We have created an array x using np.arange() function and changed the shape using the reshape() function.

`array([6, 6, 6, 1, 3, 2, 5, 4, 4, 4])`
`array([1, 1, 1, 1, 3, 2, 5, 4, 4, 4])`
`array([-4, -2, 0, 1, 3, 2, 5, 4, 4, 5])`
`array([5, 5, 5, 1, 3, 2, 5, 4, 5, 5, 5])`
`array([3, 3, 3, 1, 3, 2, 5, 4, 3, 3, 3])`
```array([
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1],
[3, 3, 3, 3, 4, 3, 3],
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 2, 1, 1]
])```

Suggestion : 6

that can be implemented using numpy and python:

```import numpy as np

A = np.arange(0, 12)

A = A.reshape(4, 3)

print(A)```

gives

```[
[0 1 2]
[3 4 5]
[6 7 8]
[9 10 11]
]```

and

`print(A.shape)`

` B = np.pad(A, (3, 3), constant_values = -99)`
` B = np.pad(A, (3, 1), constant_values = -99)`