
- 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 - Set Operations ufunc
Set Operations Universal Functions (ufunc)
NumPy provides several universal functions (ufuncs) that perform set operations on arrays. These operations are used to compare and manipulate sets of elements within arrays. The main set operations include union, intersection, difference, and exclusive-or.
NumPy Union Function
The numpy.union1d() function is used to find the union of two arrays. The union of two sets is a set containing all elements from both sets, without duplicates.
Example
In the following example, we use the numpy.union1d() function to find the union of two arrays −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([3, 4, 5]) # Find the union of the arrays union_result = np.union1d(array1, array2) print("Union of arrays:", union_result)
The output obtained is as follows −
Union of arrays: [1 2 3 4 5]
NumPy Intersection Function
The numpy.intersect1d() function is used to find the intersection of two arrays. The intersection of two sets is a set containing only the elements that are present in both sets.
Example
In the following example, we use the numpy.intersect1d() function to find the intersection of two arrays −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([3, 4, 5]) # Find the intersection of the arrays intersection_result = np.intersect1d(array1, array2) print("Intersection of arrays:", intersection_result)
The output produced is as follows −
Intersection of arrays: [3]
NumPy Set Difference Function
The numpy.setdiff1d() function is used to find the set difference of two arrays. The set difference of two sets is a set containing elements that are present in the first set but not in the second set.
Example
In this example, we use the numpy.setdiff1d() function to find the difference between two arrays −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([3, 4, 5]) # Find the set difference of the arrays difference_result = np.setdiff1d(array1, array2) print("Difference of arrays:", difference_result)
We get the following result −
Difference of arrays: [1 2]
NumPy Set Exclusive-or Function
The numpy.setxor1d() function is used to find the set exclusive-or (symmetric difference) of two arrays. The set exclusive-or of two sets is a set containing elements that are present in either of the sets but not in both.
Example
In this example, we use the numpy.setxor1d() function to find the exclusive-or of two arrays −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([3, 4, 5]) # Find the set exclusive-or of the arrays xor_result = np.setxor1d(array1, array2) print("Exclusive-or of arrays:", xor_result)
The output produced is as follows −
Exclusive-or of arrays: [1 2 4 5]
The NumPy in1d() Function
The numpy.in1d() function tests whether each element of an array is also present in a second array. It returns a boolean array of the same shape as the first array, indicating whether each element is present in the second array.
Example
In the example below, we use the numpy.in1d() function to test whether elements of one array are present in another array −
import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([3, 4, 5]) # Test whether elements of array1 are in array2 in1d_result = np.in1d(array1, array2) print("Elements of array1 in array2:", in1d_result)
The output obtained is as follows −
Elements of array1 in array2: [False False True]
The NumPy isin() Function
The numpy.isin() function tests whether each element of an array is present in a list of values or another array. It is similar to numpy.in1d() function but can be used with multi-dimensional arrays.
Example
In this example, we use the numpy.isin() function to test whether elements of an array are present in another array −
import numpy as np # Define an array and a list of values array = np.array([1, 2, 3, 4]) values = [2, 4, 6] # Test whether elements of the array are in the list of values isin_result = np.isin(array, values) print("Elements of array in values:", isin_result)
The result produced is as follows −
Elements of array in values: [False True False True]