NUMPY ARGWHERE WITH EXAMPLES IN PYTHON

Hello geeks and welcome in this article, we will cover NumPy argwhere. Along with that, we will also look at its syntax and parameters involved. For a better understanding, we will also look at various examples. But first, let us try to get a general idea about the function itself. Numpy is a powerful mathematical library of Python that provides us with many useful functions. One such useful function of NumPy is argwhere. This helps the user by providing the index number of all the non-zero elements in the matrix grouped by elements. We can use this function with a limit of our own also that we will see in examples. But at first, let us look at its syntax.

SYNTAX OF NUMPY ARGWHERE

Given below is the general syntax for this function

numpy.argwhere(a)

A very simple syntax with just one parameter involved that we will see next and then jump onto examples.

PARAMETER OF NUMPY ARGWHERE

a:array_like

The parameter “a” represents the input array over which the operation needs to be carried on. The array can be one dimensional as well as multidimensional.

RETURNS

Index_array:(N,a.ndim) ndarray

On completion of the program, it returns the index of elements that non-zero. The array will have a shape of (N,a.ndim) where N represents the number of non-zero items. Also, in the output, the indices are grouped by element.

EXAMPLES OF NUMPY ARGWHERE

Now let us look at a couple of examples that will help us in understanding the topic better. Let’s start with an elementary example, and then we will twist and turn the syntax. Which helps us in exploring and increase our familiarity with the syntax.

Example 1 – NUMPY ARGWHERE

import numpy as ppool 
a=[[1,3,0],
    [0,0,5]]
print(ppool.argwhere(a))

Output:

[[0 0]
 [0 1]
 [1 2]]

In the above example, we have first imported the NumPy module. In the next step, we have declared an array. After which, we have used our function NumPy argwhere with a motive of finding non-zero elements. In the output, we get the location of all our non-zero elements. [0,0] for 1 ,[0,1] for 3 and [1,2] for 5. Hence our program is successfully executed. The output is of the form [column, row]. Now let us look at another example where we want all the non-zero terms and satisfy another condition.

EXAMPLE 2.

import numpy as ppool
a=ppool.array([[1,20,34],
                [30,21,40]])
print(ppool.argwhere(a > 20))

Output:

[[0 2]
 [1 0]
 [1 1]
 [1 2]]

In the above example, our goal is not to find out the non-zero elements. But to find out elements that are greater than 20. To do so, we have first imported the NumPy module. In the next step, we have defined an array. Then we have used our normal syntax NumPy argwhere(array_name). But this time, we have added a condition that the number must have a value greater than 20. In the end, we get an output that justifies our program. With [0,2] for 34, [1,0] for 30 , [1,1] for 21 and [1,2] for 40. It is a beneficial function that comes in handy when dealing with big arrays.

Now let us look at one last example. Here our goal is to find only the index number of the element whose value matches our input.

EXAMPLE 3 – NUMPY ARGWHERE.

import numpy as ppool
a=ppool.array([[0,20,54],
                [60,21,40]])

print(ppool.argwhere(a==20))

Output:

[[0 1]]

Same as the above example with the only difference that this time instead of using this “>” sign. We have used “==” which means equal to (if you want to try for not equal to use this “!=”), and the value specified is 20. In our output, we get [0,1], which is the location for 20 in the matrix. So our program is justified.

Must Read

CONCLUSION

In this article, we cover NumPy argwhere. Besides that, we also looked at its syntax and parameters. For a better understanding, we looked at different examples. We played with the syntax for a bit and looked at the output in each case. I hope this article was able to clear all of your doubts. In case you still have some unsolved queries, then feel free to write them below in the comment section. Done reading this, why not read Numpy mgrid up next.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments