NumPy – Unique Elements
In data analysis, we often deal with repeated values in datasets.
To clean and analyze data effectively, we need to extract only the unique elements.
In NumPy, this is done using the powerful function np.unique().
It is widely used in:
- Data science
- Machine learning
- Data cleaning
- Statistics
- Feature engineering
What are Unique Elements?
Unique elements are values that appear only once in the result set (duplicates are removed).
Simply: It returns only distinct values from an array.
Import NumPy
import numpy as np
1. Find Unique Elements
import numpy as np
A = np.array([1, 2, 2, 3, 4, 4, 5])
result = np.unique(A)
print(result)
Output:
[1 2 3 4 5]
2. Unique Elements in 2D Array
import numpy as np
A = np.array([[1, 2, 2],
[3, 4, 4]])
print(np.unique(A))
Output:
[1 2 3 4]
3. Return Counts of Unique Elements
import numpy as np
A = np.array([1, 2, 2, 3, 3, 3, 4])
unique, counts = np.unique(A, return_counts=True)
print("Unique:", unique)
print("Counts:", counts)
Output:
Unique: [1 2 3 4]
Counts: [1 2 3 1]
4. Return Index of Unique Elements
import numpy as np
A = np.array([10, 20, 20, 30, 40])
unique, index = np.unique(A, return_index=True)
print("Unique:", unique)
print("Index:", index)
5. Return Inverse Mapping
import numpy as np
A = np.array([10, 20, 20, 30])
unique, inverse = np.unique(A, return_inverse=True)
print("Unique:", unique)
print("Inverse:", inverse)
6. Sorting Behavior
By default, NumPy sorts unique values.
import numpy as np
A = np.array([5, 2, 9, 1, 2])
print(np.unique(A))
Output:
[1 2 5 9]
Real-World Applications
1. Data Cleaning
- Remove duplicate entries
- Clean datasets
2. Machine Learning
- Feature extraction
- Encoding categorical data
3. Statistics
- Frequency analysis
- Distribution checking
4. Databases
- Unique ID extraction
- Record validation
Why Use NumPy Unique?
Using NumPy provides:
- Fast duplicate removal
- Multiple output options
- Efficient large dataset processing
- Built-in statistical utilities
Combined with Python, it becomes essential for data analysis and AI workflows.
Unique vs Set
| Method | Feature |
|---|---|
| np.unique() | NumPy optimized, supports counts & indices |
| set() | Basic Python uniqueness |
Summary
NumPy provides a powerful function to extract unique values using:
np.unique(array)
It also supports counts, indices, and inverse mapping.
Conclusion
The unique function is essential for cleaning and analyzing data efficiently. NumPy makes it fast, flexible, and powerful for real-world applications in data science and machine learning.


0 Comments