Numpy isin Function Explained With Examples in Python

Hello geeks and welcome in this article, we will cover the NumPy isin(). 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 first, let us look at the definition of our function. Suppose you got an array A and array B. You want to check how many similar elements they have. The function NumPy isin() does that for you. On completion, it returns an array of Boolean stating the result. As we moved ahead, this will become more clear to you. In the next section, we will be looking at the syntax associated with it.

Syntax

numpy.isin(element, test_elements)

This is the general syntax for our function. There are a few parameters associated with it, which we will discuss in the next section.

Parameters

1. element: array_like

This parameter represents the input array.

2. test_elements:array_like

This parameter represents the values against which each value of the input array is to be tested.

3. assume_unique:bool

This is an optional parameter. By default, it is equal to false. If it is equal to true, then it speeds up the whole calculation.

4. invert: bool

This is another optional parameter. By default, it is equal to false. In case it is equal to “true,” the output array is inverted.

Return Type

isin:ndarray 

On completion of the program, this function returns an array of Boolean with the same shape as the input array.

Examples Covering Numpy isin Function In-depth

As we are done with all the theory portions related to NumPy isin(), this section will look 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,2,3,4,5]
b=[1,3,5]
print(ppool.isin(a,b))

Output:

[ True False  True False  True]

Above, we can see a straightforward example of NumPy isin(). We have imported the NumPy module. Following this, we have defined our input array and test array. Then, we used our syntax along with a print statement to get the desired output.

Now, let us see another example and also use some optional parameters.

import numpy as ppool
a=[[1,2,3],
[2,7,8],
[3,4,5]]
b=[1,5,7]
print(ppool.isin(a,b,invert="true"))

Output:

[[False  True  True]
 [ True False  True]
 [ True  True False]]

Here, we can see another example of our function. Here, we have followed all the steps similar to the first example. With the only difference of array here, we have used an optional parameter. Here, we can see that the optional parameter diverts the results. Another point to pay attention to is that the output has a structure similar to that of the element/input array.

Difference Between Numpy isin() Vs. Numpy in1d()

In this, we will be looking at another NumPy function named NumPy in1d(). It is somewhat an alternative to that of NumPy isin(). In this section, we will try to look at the basic differences between them. Moreover, we will also understand why it is advised to NumPy isin() over the NumPy in1d() function. One of the basic differences is that when using NumPy in1d(), the element array’s shape or the parent array is not retained.

The general syntax for NumPy in1d() function

numpy.in1d(ar1, ar2)

Now let us look at an example that will make things crystal clear to us:

NUMPY.ISIN()NUMPY.IN1D()
import numpy as ppool
a=[[1,12,3],
[2,4,5]]
b=[[12,3,4],
[23,4,5,]]
print(ppool.isin(a,b))
import numpy as ppool
a=[[1,12,3],
[2,4,5]]
b=[[12,3,4],
[23,4,5,]]
print(ppool.in1d(a,b
))
[[False True True]
[False True True]]
[False True True False True True]

I hope the above example makes everything clear to you. The NumPy isin() function is newer than the .in1d() function and is preferred more.

Conclusion

In this article, we covered the NumPy isin(). 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. In the end, we can conclude that NumPy isin().

In the end, we can conclude that this function helps to find the common elements between 2 different arrays. I hope this article was able to clear all of your doubts.

But if you have any unsolved queries, feel free to write them down below in the comment section.

If you’re done reading this, why not read about polyfit next?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments