π§ βIn the world of data, knowing where and how to look is as important as having the data.β
Welcome to Chapter 9 of our journey through the power-packed world of NumPy.
By now, you know how to create arrays, reshape them, filter using Boolean logic, and manipulate data like a pro. But when your datasets grow in size, so does the need for structure and searchability.
Thatβs where sorting and searching come into play.
In this chapter, you’ll learn how to:
-
Sort arrays efficiently
-
Find the positions of elements
-
Use functions like
sort()
,argsort()
,searchsorted()
, andnonzero()
These are key skills in:
-
Ranking data
-
Locating items
-
Preparing for binary search
-
Building recommendation systems
Letβs get started! π
π 1. Sorting with np.sort()
πΈ Basic Usage
Output:
π― np.sort()
returns a sorted copy β your original array stays unchanged.
π§± 2. Sorting Multi-Dimensional Arrays
You can sort row-wise or column-wise using the axis
parameter.
Output:
π 3. In-Place Sorting with .sort()
π§ 4. Getting Sort Indices with np.argsort()
argsort()
returns the indices that would sort an array.
Output:
β This is super useful when you want to sort related arrays based on one arrayβs values.
π― 5. Finding Elements: np.searchsorted()
Used to find the index where an element should be inserted to maintain order.
π Example:
You can also insert multiple values:
print(np.searchsorted(arr, [5, 35])) # Output: [0 3]
π 6. Finding Specific Elements with np.where()
You can use where()
to locate indices of matching conditions.
Output:
You can also find multiple matches:
π§© 7. Finding Non-Zero Values: np.nonzero()
This function returns the indices of non-zero elements in the array.
Output:
Useful in:
-
Sparse matrices
-
Filtering
-
Counting actual data points
π§ͺ Real-World Use Cases
Task | Function |
---|---|
Sort sales data | np.sort() |
Rank test scores | np.argsort() |
Find top N performers | argsort() + slicing |
Insert item in order | searchsorted() |
Locate missing/active values | where() , nonzero() |
π¦ BONUS: Sort by Another Array (Key-Based Sort)
Letβs say you have names and scores:
To sort names by scores:
Output:
β οΈ Common Mistakes to Avoid
Mistake | Fix |
---|---|
Using .sort() expecting return value |
It sorts in place, use np.sort() for copy |
Forgetting to reverse for descending | Use [::-1] or multiply array by -1 |
Confusing indices with values | Use argsort() to get indices, not values |
Using searchsorted() on unsorted array |
Always sort before using it |
π§Ύ Summary Table: Sorting and Searching
Function | Purpose |
---|---|
np.sort() |
Returns a sorted copy |
array.sort() |
Sorts array in place |
np.argsort() |
Returns indices that would sort |
np.searchsorted() |
Finds insert position in sorted array |
np.where() |
Finds indices matching condition |
np.nonzero() |
Finds indices of non-zero values |
π Wrapping Up Chapter 9
Congratulations! π
You now know how to:
-
Sort arrays (even by another arrayβs order)
-
Extract top and bottom performers
-
Find or insert elements in a sorted context
Sorting and searching are critical in:
-
Data analytics
-
Ranking systems
-
Indexing databases
-
Signal processing
-
Machine learning pre-processing
π Next in Chapter 10: Random Numbers and Simulations
Weβll explore:
-
np.random
module -
Generating random numbers
-
Simulating dice rolls, experiments, and more