# 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.

Contents

## 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 be discussing 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 against.

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, that has same shape as that of the input array.

## Examples Covering Numpy isin Function In-depth

As we are done with all the theory portion related to NumPy isin(). 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,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(). Here we have imported the NumPy module at first. Following this, we have defined our input array and test array. Then we have used our syntax along with a print statement to get the desired output.

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

```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 and 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.

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:

I hope the above example makes everything clear to you. The NumPy isin() function is newer as compared to the .in1d() function as well as it 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 in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about polyfit next.

Subscribe
Notify of 