🐍 NumPy – Advanced Indexing
NumPy provides powerful advanced indexing techniques that go beyond simple element access and slicing.
Advanced indexing allows you to:
- Select multiple elements at once
- Filter data using conditions
- Extract complex patterns
- Modify specific elements efficiently
It is widely used in:
- Data science
- Machine learning
- Data filtering
- Image processing
- Feature engineering
What is Advanced Indexing?
Advanced indexing refers to techniques that allow non-continuous or conditional selection of array elements.
It mainly includes:
- Fancy Indexing
- Boolean Indexing
- Conditional Filtering
🔵 Fancy Indexing
Fancy indexing allows you to select multiple elements using a list of indices.
Example
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
result = arr[[0, 2, 4]]
print(result)Output:
[10 30 50]✔ You can select elements in any order
Fancy Indexing in 2D Arrays
arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
result = arr[[0, 2]]
print(result)Output:
[[1 2 3]
[7 8 9]]✔ Selects entire rows
🟢 Boolean Indexing
Boolean indexing selects elements based on conditions.
Example
arr = np.array([10, 15, 20, 25, 30])
result = arr[arr > 20]
print(result)Output:
[25 30]Multiple Conditions
arr = np.array([10, 15, 20, 25, 30, 35])
result = arr[(arr > 15) & (arr < 35)]
print(result)Output:
[20 25 30]Using OR Condition
result = arr[(arr < 15) | (arr > 30)]
print(result)Output:
[10 35]🔴 Modifying Values with Boolean Indexing
You can directly update selected values.
arr = np.array([10, 20, 30, 40, 50])
arr[arr > 30] = 99
print(arr)Output:
[10 20 30 99 99]🟣 Fancy Indexing with 2D Arrays
arr = np.array([
[10, 20],
[30, 40],
[50, 60]
])
result = arr[[0, 2], [1, 0]]
print(result)Output:
[20 50]✔ Picks (0,1) and (2,0)
🟡 Conditional Filtering
arr = np.array([5, 12, 18, 25, 40])
even = arr[arr % 2 == 0]
print(even)Output:
[12 18 40]🔵 Combining Fancy + Boolean Indexing
arr = np.array([10, 20, 30, 40, 50])
result = arr[[True, False, True, False, True]]
print(result)Output:
[10 30 50]🧠 Difference Between Basic and Advanced Indexing
| Feature | Basic Indexing | Advanced Indexing |
|---|---|---|
| Access type | Single element | Multiple elements |
| Condition support | No | Yes |
| Flexibility | Low | High |
| Performance | Fast | Slightly slower |
| Use case | Simple access | Filtering & selection |
⚡ Real-World Example
Filtering student scores
scores = np.array([45, 67, 89, 30, 90, 55])
passed = scores[scores >= 50]
print(passed)Output:
[67 89 90 55]✔ Common in data analytics
🖼️ Image Processing Example
image = np.array([
[100, 150, 200],
[50, 80, 120],
[90, 60, 30]
])
bright_pixels = image[image > 100]
print(bright_pixels)Output:
[150 200 120]✔ Used in computer vision
🚀 Performance Tips
- Boolean indexing is very efficient for filtering
- Avoid unnecessary fancy indexing on large datasets
- Use vectorized conditions instead of loops
- Combine conditions for better performance
⚠️ Common Errors
1. Shape mismatch
IndexError: shape mismatch✔ Fix: Ensure index arrays match dimensions
2. Using Python lists instead of NumPy arrays
✔ Always convert data to NumPy arrays for advanced indexing
🧾 Summary
NumPy advanced indexing includes:
- Fancy indexing → select multiple indices
- Boolean indexing → filter using conditions
- Conditional indexing → apply logical operations
These techniques are essential for:
- Data filtering
- Machine learning preprocessing
- Data transformation
- Image analysis
🏁 Conclusion
Advanced indexing in NumPy provides powerful and flexible ways to access and manipulate data. It allows you to write clean, efficient, and highly expressive code for real-world data science and machine learning applications.


0 Comments