The set difference operation is used in sets to find elements in one set that are not present in the other set. For example, for two sets a and be, `a-b`

results in a set with elements of set a that are not present in set b. In this tutorial, we will look at how to apply the set difference operation between two numpy arrays with the help of some examples.

## Numpy `setdiff1d()`

Function

The numpy `setdiff1d()`

function is used to find the set difference of two arrays. The following is the syntax:

import numpy as np diff = np.setdiff1d(ar1, ar2, assume_unique=False)

It returns a numpy array with the unique values in the first array that are not present in the second array. For numpy arrays of higher dimensions (2 or more), the arrays are first flattened, and then the set difference operation is applied.

Also, if the input arrays contain unique values you can pass `True`

to the `assume_unique`

parameter. This can speed up the calculation. Note that it is `False`

by default.

Let’s look at some examples of using this function to find the set difference.

### Set difference between two 1d numpy arrays

Pass the two arrays as arguments to the numpy sefdiff1d() function.

import numpy as np # create two 1d arrays ar1 = np.array([3, 1, 1, 2, 0, 2]) ar2 = np.array([2, 3, 3, 4]) # set difference ar1 - ar2 ar1_2_diff = np.setdiff1d(ar1, ar2) # display the array print(ar1_2_diff) # display the type print(type(ar1_2_diff))

Output:

[0 1] <class 'numpy.ndarray'>

The returned array contains elements of ar1 that are not present in ar2.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Note that the set difference operation `a-b`

is not the same as `b-a`

. Thus, the order in which you pass the two arrays matters. For example, to get the elements of ar2 that are not in ar1, pass ar2 as the first array and ar1 as the second array.

# create two 1d arrays ar1 = np.array([3, 1, 1, 2, 0, 2]) ar2 = np.array([2, 3, 3, 4]) # set difference ar2 - ar1 ar2_1_diff = np.setdiff1d(ar2, ar1) # display the array print(ar2_1_diff)

Output:

[4]

We get the elements of ar2 that are not in ar1.

If the input arrays are unique, you can pass `assume_unique=True`

to speed up the calculation.

# create two 1d arrays ar1 = np.array([3, 1, 2, 0]) ar2 = np.array([2, 3, 4]) # set difference ar1 - ar2 ar1_2_diff = np.setdiff1d(ar1, ar2, assume_unique=True) # display the array print(ar1_2_diff)

Output:

[1 0]

We get the same output as above. Note that with `assume_unique=True`

the resulting array is not sorted.

### Set difference between two 2d numpy arrays

If the input arrays are not 1d, they will be flattened before calculating the set difference. For example, if we apply the np.setdiff1d() function on two 2d arrays –

# create two 2d arrays ar1 = np.array([[1, 2, 3], [4, 4, 1]]) ar2 = np.array([[3, 4, 3], [5, 6, 7]]) # set difference ar1 - ar2 ar1_2_diff = np.setdiff1d(ar1, ar2) # display the array print(ar1_2_diff)

Output:

[1 2]

We get a 1d numpy array with elements of ar1 that are not present in ar2.

For more on the numpy setdiff1d() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**