NumPy Sort, Search & Counting Functions
NumPy provides powerful functions for:
- Sorting data
- Searching values
- Counting occurrences
These operations are essential in data analysis, machine learning, scientific computing, and data preprocessing.
NumPy makes sorting, searching, and counting operations extremely fast compared to standard Python lists.
Why Use Sort, Search & Counting Functions?
They help you:
- Organize data efficiently
- Locate specific values quickly
- Filter datasets
- Analyze large datasets
- Prepare data for machine learning
NumPy Sorting Functions
What is Sorting?
Sorting arranges array elements in ascending or descending order.
1. Using sort()
Syntax
np.sort(array)
Example
import numpy as np
arr = np.array([50, 10, 30, 20, 40])
print(np.sort(arr))
Output
[10 20 30 40 50]
Sorting a 2D Array
import numpy as np
arr = np.array([
[30, 10, 20],
[60, 40, 50]
])
print(np.sort(arr))
Output
[[10 20 30]
[40 50 60]]
2. Descending Sort
import numpy as np
arr = np.array([50, 10, 30, 20, 40])
print(np.sort(arr)[::-1])
Output
[50 40 30 20 10]
3. Using argsort()
Returns indices that would sort the array.
import numpy as np
arr = np.array([50, 10, 30])
print(np.argsort(arr))
Output
[1 2 0]
Explanation
Index 1 → 10
Index 2 → 30
Index 0 → 50
NumPy Searching Functions
Searching helps locate values within arrays.
4. Using where()
Returns positions matching a condition.
import numpy as np
arr = np.array([10, 20, 30, 40])
result = np.where(arr == 30)
print(result)
Output
(array([2]),)
5. Search Values Greater Than 25
import numpy as np
arr = np.array([10, 20, 30, 40])
print(np.where(arr > 25))
Output
(array([2, 3]),)
6. Using searchsorted()
Finds insertion position in a sorted array.
import numpy as np
arr = np.array([10, 20, 30, 40])
print(np.searchsorted(arr, 25))
Output
2
Explanation
25 should be inserted at index 2.
Multiple Search Values
import numpy as np
arr = np.array([10, 20, 30, 40])
print(np.searchsorted(arr, [15, 35]))
Output
[1 3]
NumPy Counting Functions
Counting functions help analyze datasets.
7. Using count_nonzero()
Counts non-zero values.
import numpy as np
arr = np.array([1, 0, 2, 0, 3, 4])
print(np.count_nonzero(arr))
Output
4
8. Count Specific Values
import numpy as np
arr = np.array([10, 20, 10, 30, 10])
count = np.sum(arr == 10)
print(count)
Output
3
9. Count Boolean Conditions
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
count = np.sum(arr > 25)
print(count)
Output
3
10. Using unique()
Returns unique values and counts.
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3])
values, counts = np.unique(arr, return_counts=True)
print(values)
print(counts)
Output
[1 2 3]
[1 2 3]
Practical Example: Student Scores
import numpy as np
scores = np.array([85, 90, 70, 90, 95, 70])
unique_scores, counts = np.unique(
scores,
return_counts=True
)
print(unique_scores)
print(counts)
Output
[70 85 90 95]
[ 2 1 2 1]
Practical Example: Product Sales
import numpy as np
sales = np.array([200, 450, 300, 500, 250])
print(np.sort(sales))
print(np.where(sales > 300))
Output
[200 250 300 450 500]
(array([1, 3]),)
Comparison of Functions
| Function | Purpose |
|---|---|
sort() | Sort values |
argsort() | Get sorting indices |
where() | Find matching positions |
searchsorted() | Find insertion index |
count_nonzero() | Count non-zero values |
unique() | Find unique values |
sum(condition) | Count matches |
Real-World Applications
These functions are widely used in:
- Data cleaning
- Machine learning preprocessing
- Financial analysis
- Statistical analysis
- Database querying
- Image processing
- Scientific computing
Advantages of NumPy Sort, Search & Counting Functions
- Extremely fast
- Optimized for large datasets
- Easy-to-use syntax
- Memory efficient
- Essential for analytics and AI
Summary
NumPy provides powerful tools for sorting, searching, and counting data. Functions such as sort(), argsort(), where(), searchsorted(), count_nonzero(), and unique() help you efficiently analyze and manipulate arrays.
These functions are core features of NumPy and are extensively used in data science applications built with Python.
Conclusion
Mastering NumPy sort, search, and counting functions will significantly improve your ability to organize, filter, and analyze data efficiently. These tools form the foundation of many real-world data processing and machine learning workflows.


0 Comments