
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Difference
Difference in NumPy
In NumPy, the difference operation is used to find elements present in one array but not in another. It is commonly used to compare two arrays and identify the unique elements in one array that do not exist in the other.
In NumPy, the setdiff1d() function is used to perform this operation.
What is Set Difference?
The "set difference" operation refers to finding the elements that are present in one set but not in another. In NumPy, this operation is applied to arrays, and it returns the elements in the first array that are not found in the second array.
This concept is closely related to the mathematical set theory, where the difference of two sets A - B contains elements in set A but not in set B.
For example, given two arrays −
array1 = [1, 2, 3, 4, 5] array2 = [3, 4, 5, 6, 7]
The set difference will give us the elements in array1 that are not in array2, which are [1, 2].
Syntax
Following is the basic syntax for the setdiff1d() function in NumPy −
numpy.setdiff1d(ar1, ar2)
Where,
- ar1: The first input array. It is the array from which we want to subtract elements.
- ar2: The second input array. It contains elements that will be removed from the first array.
The result is a sorted array containing the unique values that are in ar1 but not in ar2.
Example
In the following example, we are calculating the difference between two arrays using the setdiff1d() function in NumPy −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3, 4, 5]) array2 = np.array([3, 4, 5, 6, 7]) # Find the difference between the two arrays difference = np.setdiff1d(array1, array2) print("Difference between array1 and array2:", difference)
Following is the output obtained −
Difference between array1 and array2: [1 2]
Handling Arrays with Duplicate Elements
If the input arrays contain duplicate elements, the numpy.setdiff1d() function will remove the duplicates before performing the difference operation. This ensures that the result contains only unique values.
Example
Here, we are removing the duplicates in array1 before computing the difference, resulting in the final output containing only the unique elements −
import numpy as np # Define arrays with duplicate elements array1 = np.array([1, 2, 2, 3, 4]) array2 = np.array([3, 4, 4, 5, 6]) # Find the difference between the arrays difference = np.setdiff1d(array1, array2) print("Difference with duplicates removed:", difference)
The result produced is as follows −
Difference with duplicates removed: [1 2]
Handling Arrays with Different Data Types
NumPys setdiff1d() function works with arrays of different data types, including integers, floats, and strings.
However, the function will automatically convert the data types to a common type before performing the difference operation.
Example
Let us take a look at an example where we calculate the difference between an integer array and a float array −
import numpy as np # Define arrays with different data types array1 = np.array([1, 2, 3, 4.5]) array2 = np.array([4.5, 5, 6]) # Find the difference between the arrays difference = np.setdiff1d(array1, array2) print("Difference with different data types:", difference)
After executing the above code, we get the following output −
Difference with different data types: [1. 2. 3.]
Difference with Multiple Arrays
In NumPy, you can only use setdiff1d() function to compute the difference between two arrays at a time.
If you want to compute the difference with multiple arrays, you can use a combination of setdiff1d() function and loops or reduce() function from the functools module.
Example
Following is an example that demonstrates how to calculate the difference between multiple arrays −
import numpy as np from functools import reduce # Define multiple arrays array1 = np.array([1, 2, 3, 4, 5]) array2 = np.array([3, 4, 5, 6, 7]) array3 = np.array([5, 6, 7, 8]) # Calculate the difference of all arrays difference = reduce(lambda x, y: np.setdiff1d(x, y), [array1, array2, array3]) print("Difference of multiple arrays:", difference)
The output obtained is as shown below −
Difference of multiple arrays: [1 2]
Performance Considerations
The numpy.setdiff1d() function is quite efficient, but performance can be a consideration when dealing with large arrays.
If your arrays contain only unique elements, you can use the assume_unique parameter to speed up the computation.
Example
By setting theassume_unique parameter to True, NumPy optimizes the operation when dealing with arrays that already contain unique elements, leading to faster performance as shown in the example below −
import numpy as np # Define arrays with unique elements array1 = np.array([1, 2, 3, 4, 5]) array2 = np.array([3, 4, 5, 6, 7]) # Find the difference assuming unique elements difference = np.setdiff1d(array1, array2, assume_unique=True) print("Difference with unique elements:", difference)
The result produced is as follows −
Difference with unique elements: [1 2]