Hello geeks and welcome in today’s article, we will discuss NumPy insert(). Along with it, we will also cover its syntax and parameters. For a better understanding, we will also look at a couple of examples. NumPy is a powerful mathematical library of python which provides us with a function insert. NumPy insert() helps us by allowing us to insert values in a given axis before the given index number. To execute this operation, there are several parameters that we need to take care of. One such thing is the axis; if not defined, then the input array is flattened first. We will read more about parameters as we move ahead in the article.
SYNTAX OF NUMPY INSERT()
Given below is the syntax of NumPy.insert()
numpy.insert
(arr, obj, values, axis=None)
This is the standard syntax that has a total of 4 parameters. Each parameter is important in its own ways. Up next, we will cover each parameter in detail.
PARAMETER OF NUMPY INSERT()
arr: array_like
This represents the input array on which the operation has to be carried.
obj: int, slice
This represents the index before which the new value needs to insert.
values: array_like
This parameter represents the value to be inserted in the array. The value to be inserted must have a data type similar to that of the array, or else it undergoes a conversion.
Axis: int
It is an optional parameter, and as stated above, if not defined, the input array is flattened. This means that the input array is converted to a one 1 dimensional array.
RETURN
OUT: ndarray
It returns an input array with the new value inserted.
EXAMPLES OF NUMPY INSERT()
Now let us look at couple of examples that will help us in understanding of Numpy insert()
import numpy as ppool a=ppool.array([[2,4],[6,8],[10,12]]) print(a) print(ppool.insert(a,1,5))
#output [[ 2 4] [ 6 8] [10 12]] #without axis [ 2 5 4 6 8 10 12]
EXPLANATION
Here in the above example, we have imported NumPy first. In the next step, we have defined the array can be termed as the input array. On which all the operations will be performed. Then following the proper syntax we have written: “ppool.insert(a,1,5)“. Here the notation “a” represents the array name, “1” represents the index at which the value needs to be inserted. “5” represents the value to be inserted, and output justifies it. One more important thing to take note of is that here we have not defined any axis. As a result of which we get a flattened array ( 1 dimension) as an output.
Now let us look at another example
import numpy as ppool a=ppool.array([[2,4],[6,8],[10,12]]) print(a) print(ppool.insert(a,1,5,axis=1))
#output [[ 2 4] [ 6 8] [10 12]] #with axis [[ 2 5 4] [ 6 5 8] [10 5 12]]
The above example is the same as the one we dealt with earlier. With the only difference of axis and if you compare the two outputs, change is quite evident. Here we don’t get any 1-dimensional matrix. Rather at every 1st location of the matrix, the value is inserted.
Now lets look at one last example in which we will insert elements using a sequence.
import numpy as ppool a=ppool.array([[2,4],[6,8],[10,12]]) print(ppool.insert(a,1,[5,2,3],axis=1))
#output [[ 2 5 4] [ 6 2 8] [10 3 12]]
Here we can see in the same example we have used a sequence. Instead of adding a similar value, we have added different values at index number 1 of sub-arrays.
MUST-READ
- Numpy Mean: Implementation and Importance
- WHAT IS NUMPY DIFF? ALONG WITH EXAMPLES
- Working With Python Bisect Module
- Python Max Int | What’s the Maximum Value of int Data Type in Python
- Python int to Binary | Integer to Binary Conversion
CONCLUSION
In this article, we covered the NumPy insert. For a better understanding, we looked at its syntax, parameters. We looked at a couple of examples and played with the syntax a bit. In the end, we conclude that NumPy insert() helps us insert values along the given axis. I hope this article was able to clear all of your doubts.
In case if you have any unsolved doubts feel free to write them below in the comment section. Done reading this why not read NumPy trace next.
The post NUMPY INSERT IN PYTHON WITH EXAMPLES appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment