Hello, readers! In this article, we will learn the universal NumPy Set Operations in Python. So, let us get started! 🙂

## Useful Numpy set operations

We’re going over 5 useful numpy set operations in this article.

`numpy.unique(array)`

`numpy.union1d(array,array)`

`numpy.intersect1d(array,array,assume_unique)`

`np.setdiff1d(arr1, arr2, assume_unique=True)`

`np.setxor1d(arr1, arr2, assume_unique=True)`

Let’s check these operations individually.

### 1. Unique values from a NumPy Array

This numpy set operation helps us find unique values from the set of array elements in Python. The `numpy.unique()`

function skips all the duplicate values and represents only the unique elements from the Array

**Syntax:**

```
numpy.unique(array)
```

**Example:**

In this example, we have used unique() function to select and display the unique elements from the set of array. Thus, it skips the duplicate value 30 and selects it only once.

```
import numpy as np
arr = np.array([30,60,90,30,100])
data = np.unique(arr)
print(data)
```

**Output:**

```
[ 30 60 90 100]
```

### 2. Set union operation on NumPy Array

NumPy offers us with universal `union1d()`

function that performs UNION operation on both the arrays.

That is, it clubs the values from both the arrays and represents them. This process completely neglects the duplicate values and includes only a single occurrence of the duplicate element in the UNION set of arrays.

**Syntax:**

```
numpy.union1d(array,array)
```

**Example:**

```
import numpy as np
arr1 = np.array([30,60,90,30,100])
arr2 = np.array([1,2,3,60,30])
data = np.union1d(arr1,arr2)
print(data)
```

**Output:**

```
[ 1 2 3 30 60 90 100]
```

### 3. Set Intersection operation on NumPy array

The `intersect1d() function`

enables us to perform INTERSECTION operation on the arrays. That is, it selects and represents the common elements from both the arrays.

**Syntax:**

```
numpy.intersect1d(array,array,assume_unique)
```

- assume_unique: If set to TRUE, it includes the duplicate values for intersection operation. Setting it to FALSE, would result in the neglection of duplicate values for intersection operation.

**Example:**

Here, as we have set `assume_unique`

to TRUE, the intersection operation has been performed including the duplicate values i.e. it selects the common values from both the arrays including the duplicates of those common elements.

```
import numpy as np
arr1 = np.array([30,60,90,30,100])
arr2 = np.array([1,2,3,60,30])
data = np.intersect1d(arr1, arr2, assume_unique=True)
print(data)
```

**Output:**

```
[30 30 60]
```

## 4. Finding uncommon values with NumPy Array

With `setdiff1d()`

function, we can find and represent all the elements from the 1st array that are not present in the 2nd array according to the parameters passed to the function.

```
import numpy as np
arr1 = np.array([30,60,90,30,100])
arr2 = np.array([1,2,3,60,30])
data = np.setdiff1d(arr1, arr2, assume_unique=True)
print(data)
```

**Output:**

```
[ 90 100]
```

## 5. Symmetric Differences

With `setxor1d()`

function, we can calculate the symmetric differences between the array elements. That is, it selects and represents all the elements that are not common in both the arrays. Thus, it omits all the common values from the arrays and represents the distinct values with respect to both the arrays.

**Example:**

```
import numpy as np
arr1 = np.array([30,60,90,30,100])
arr2 = np.array([1,2,3,60,30])
data = np.setxor1d(arr1, arr2, assume_unique=True)
print(data)
```

**Output:**

```
[ 1 2 3 90 100]
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 🙂