Numpy np.unique() method
0 129
Introduction
In data analysis and scientific computing, identifying unique elements in datasets is a fundamental task. NumPy, a powerful library for numerical computations in Python, provides the np.unique()
method to efficiently find unique elements in an array. This method is versatile and can be applied to both one-dimensional and multi-dimensional arrays.
Syntax of np.unique()
The basic syntax of the np.unique()
method is:
numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None, equal_nan=True)
Where:
ar
: Input array.return_index
: If True, returns the indices of the unique elements in the original array.return_inverse
: If True, returns the indices to reconstruct the original array from the unique array.return_counts
: If True, returns the number of times each unique element appears in the original array.axis
: The axis along which to operate. If None, the array is flattened before operation.equal_nan
: If True, treats NaNs as equal during comparison.
Basic Example
Let's start with a simple example to find unique elements in a one-dimensional array:
import numpy as np
arr = np.array([1, 2, 2, 4, 3, 6, 4, 8])
unique_elements = np.unique(arr)
print("Unique elements:", unique_elements)
Output:
Unique elements: [1 2 3 4 6 8]
Handling Multi-dimensional Arrays
For multi-dimensional arrays, np.unique()
can be used to find unique elements across the entire array or along a specific axis:
arr_2d = np.array([[10.2, 21.4, 3.6, 14.8], [1.0, 5.0, 10.0, 15.0]])
unique_elements_2d = np.unique(arr_2d)
print("Unique elements in 2D array:", unique_elements_2d)
Output:
Unique elements in 2D array: [ 1. 3.6 5. 10. 10.2 14.8 15. 21.4]
Advanced Features
The np.unique()
method also offers advanced features:
return_index=True
: Returns the indices of the unique elements in the original array.return_inverse=True
: Returns the indices to reconstruct the original array from the unique array.return_counts=True
: Returns the number of times each unique element appears in the original array.
Example:
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique_elements, indices, inverse_indices, counts = np.unique(arr, return_index=True, return_inverse=True, return_counts=True)
print("Unique elements:", unique_elements)
print("Indices:", indices)
print("Inverse indices:", inverse_indices)
print("Counts:", counts)
Output:
Unique elements: [1 2 3 4]
Indices: [0 1 3 6]
Inverse indices: [0 1 1 2 2 2 3]
Counts: [1 2 3 1]
Using the axis
Parameter
To find unique rows or columns in a multi-dimensional array, use the axis
parameter:
arr_2d = np.array([[1, 2], [2, 3], [1, 2], [2, 3]])
unique_rows = np.unique(arr_2d, axis=0)
print("Unique rows:", unique_rows)
Output:
Unique rows: [[1 2]
[2 3]]
Conclusion
The np.unique()
method is a powerful tool in NumPy for identifying unique elements in arrays. Whether working with one-dimensional or multi-dimensional data.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments