
- 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 - Union of Arrays
Union of Arrays in NumPy
The union of arrays in NumPy refers to combining multiple arrays into a single array while removing duplicate elements. This ensures that each element appears only once in the array. In NumPy, we can achieve this using the union1d() function.
The union of arrays in NumPy is similar to the union operation in set theory where all unique elements from multiple sets are combined into one set.
Using union1d() Function
The np.union1d() function in NumPy is used to compute the union of two arrays. This function returns a sorted array of unique values that are present in either of the input arrays. Following is the syntax −
numpy.union1d(arr1, arr2)
Where,
- arr1 − It is the first input array. This array can be of any shape or data type, but typically is a 1D array for simplicity.
- arr2 − It is the second input array. It should have the same data type as "arr1".
Example
In the following example, we are using the union1d() function to find the union of 2 arrays "arr1" and "arr2" −
import numpy as np # Define two arrays arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([3, 4, 5, 6]) # Compute the union of the two arrays union_result = np.union1d(arr1, arr2) print("Union of two arrays:", union_result)
Following is the output obtained −
Union of two arrays: [1 2 3 4 5 6]
Union of Multiple Arrays
To find the union of more than two arrays, you can use union1d() function multiple times. This involves applying the function iteratively to pairs of arrays until all arrays have been included in the union.
Alternatively, you can use functions like np.concatenate() along with np.unique(). Following is the syntax −
numpy.concatenate((array1, array2, ...)) numpy.unique(array)
Example: Sequential Union
In the example below, we are finding the union of 3 arrays iteratively using the union1d() function −
import numpy as np # Define multiple 1D arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([2, 3, 4]) arr3 = np.array([4, 5, 6]) # Compute the union of three arrays # Union of first two arrays union_temp = np.union1d(arr1, arr2) # Union with the third array union_result = np.union1d(union_temp, arr3) print("Union of multiple arrays (sequential):", union_result)
This will produce the following result −
Union of multiple arrays (sequential): [1 2 3 4 5 6]
Example: Using np.concatenate() and np.unique() Functions
In this example, we first concatenate all arrays into a single array. Then, we extract the unique elements from this concatenated array using the unique() function −
import numpy as np # Define multiple 1D arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([2, 3, 4]) arr3 = np.array([4, 5, 6]) # Concatenate all arrays into one concatenated_array = np.concatenate((arr1, arr2, arr3)) # Find unique elements union_result = np.unique(concatenated_array) print("Union of multiple arrays (concatenate and unique):", union_result)
Following is the output of the above code −
Union of multiple arrays (concatenate and unique): [1 2 3 4 5 6]
Handling Multi-dimensional Arrays
We can also apply union operations to multi-dimensional arrays. To perform union operations on multi-dimensional arrays, you need to flatten the arrays first.
Flattening transforms a multi-dimensional array into a 1D array, allowing you to perform union operations as if the arrays were one-dimensional. After performing the union operation, you can reshape the result back into the original dimensions if needed.
In NumPy, multi-dimensional arrays are arrays with more than one dimension. They are commonly referred to as "ndarrays" and are used to represent complex data structures like matrices or higher-dimensional tensors.
Managing these arrays involves understanding their structure, performing operations, and efficiently manipulating data.
Example
In this example, we first define two 2D arrays "arr1" and "arr2". We then flatten them into 1d arrays using the flatten() function and then compute their union −
import numpy as np # Define 2D arrays arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[3, 4], [5, 6]]) # Flatten the arrays and compute the union flattened_arr1 = arr1.flatten() flattened_arr2 = arr2.flatten() union_result = np.union1d(flattened_arr1, flattened_arr2) print("Union of 2D arrays:", union_result)
The output obtained is as shown below −
Union of 2D arrays: [1 2 3 4 5 6]
Union with Complex Data Types
Union operations with complex data types in NumPy involve working with arrays that have structured or object-oriented data. Unlike simple numeric arrays, complex data types can include fields such as integers, floats, strings, and even other arrays.
The np.union1d() function can handle arrays of any data type as long as they are comparable.
Example
In the following example, we first create 2 structured arrays and then combine them using the union1d() function, removing duplicates and keeping the unique entries −
import numpy as np # Define structured arrays arr1 = np.array([(1, 'a'), (2, 'b')], dtype=[('num', 'i4'), ('letter', 'S1')]) arr2 = np.array([(2, 'b'), (3, 'c')], dtype=[('num', 'i4'), ('letter', 'S1')]) # Compute the union of structured arrays union_result = np.union1d(arr1, arr2) print("Union of structured arrays:", union_result)
After executing the above code, we get the following output −
Union of structured arrays: [(1, b'a') (2, b'b') (3, b'c')]