Sunday, January 3, 2021

Python Pool: Numpy Pad Explained With Examples in Python

Hello geeks and welcome in this article, we will cover the NumPy pad(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples.

But at first, let us try to get a brief understanding of the function through its definition. As we know that NumPy is a powerful mathematical library of python. It provides us with a function called NumPy pad(), which adds padding in the arrays. The definition that we just discussed here would become more clear as we move further in this article. In the next section, we will be covering the syntax associated with the function.

SYNTAX OF NUMPY PAD()

numpy.pad(array, pad_width, mode='')

This is the general syntax for our function. There are several parameters associated with it which we will be discussing in the next section.

PARAMETER OF NUMPY PAD()

1.array: array_like

This represents the input array on which the padding needs to be performed.

2. pad_width:{sequence, array_like, int}

This parameter represents the number of values padded to edge of each axis.

3. mode: str or functional

It represents either one of the following string values or a user supplied function.

  • constant: In this case, the padding takes place with a constant value.
  • edge: In this case, padding takes place with the edge value of an array.
  • maximum: In this case, the padding takes the maximum value of all vector parts along the given axis.
  • minimum: In this case, the padding takes the minimum value of all vector parts along the given axis.
  • mean: In this case, the padding takes the mean value of all parts of the vector along the given axis.
  • median: In this case, the padding takes the maximum value of all vector parts along the given axis.
  • symmetric: In this case, the padding occurs along with the vector mirrored reflection along the array’s edge.
  • reflect: In this case, the Padding takes place with the vector’s reflection on the first and last values along each axis.

4. stat_length: sequence or int

This parameter is used in ‘maximum,’ ‘mean,’ ‘median,’ and ‘minimum.’ Here, the number of values at the edge of each axis is used to calculate the statistic value.

5. constant values: sequence or scalar

This parameter is used in ‘constant’. Here the values are used to set the padded values for each axis.

6. reflect_type:{even, odd}

This parameter is used with the reflect and symmetric type.

EXAMPLES

As we are done with all the theory portion related to NumPy pad(), in this section, we will be looking at how this function works and how it helps us achieve our desired output. We will start with an elementary level example and gradually move our way to more complicated examples.

#input
import numpy as ppool
a=[1,23,4,5]
print(ppool.pad(a,(2,3),"maximum"))

Output:

[23 23  1 23  4  5 23 23 23]

In the above example, at first, we have imported the NumPy module. After which, we have defined an input on which the operation needs to be performed. Then we have used our syntax to get the desired output. In this example, we have used “Maximum” as our choice for mode. As a result of which we get 23(max value) padded at the front and the back.

Now let us look at one more example with a different mode.

#input
import numpy as ppool
a=[18,21,32,43,50,70,4]
print(ppool.pad(a,(5,1),"reflect"))

Output:

[70 50 43 32 21 18 21 32 43 50 70  4 70]

A similar example to that of the first one. But here, we have used a different array. Also, here we have used “reflect” as our mode for observation. We can spot the difference in the output.

I tried out for the “maximum” and “reflect” mode. Why don’t you guys try out for the “mean” mode of observation and tell me your results in the comment section?

MUST READ

CONCLUSION

In this article, we covered the NumPy pad(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. We can conclude that the NumPy pad() helps us add padding in the array.  I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read Memmap next.

The post Numpy Pad Explained With Examples in Python appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...