π Introduction
When working with datasets, you often need to filter specific values.
Thatβs where Boolean Indexing in NumPy becomes extremely powerful.
π What is Boolean Indexing?
Boolean indexing allows you to:
- Filter data based on conditions
- Extract only required values
- Perform dynamic queries
π§ͺ Example Array
import numpy as nparr = np.array([10, 20, 30, 40, 50])
β 1. Basic Boolean Condition
print(arr > 25)
π Output:
[False False True True True]
β Returns True/False for each element
π 2. Filtering Values
print(arr[arr > 25])
π Output:
[30 40 50]
β Only values satisfying condition are returned
π 3. Multiple Conditions
print(arr[(arr > 20) & (arr < 50)])
π Output:
[30 40]
β Operators:
&β AND|β OR
π 4. Boolean Indexing in 2D Arrays
arr = np.array([[1,2,3],[4,5,6]])print(arr[arr > 3])
π Output:
[4 5 6]
π 5. Modify Values Using Condition
arr = np.array([10, 20, 30, 40])arr[arr > 25] = 0
print(arr)
π Output:
[10 20 0 0]
β Replace values dynamically
β‘ Why Boolean Indexing is Powerful?
Using NumPy, you can:
- Avoid loops
- Filter large datasets instantly
- Write clean & efficient code
π¦ Real-World Use Cases
Boolean indexing is widely used in:
- Data cleaning
- Filtering datasets
- Machine learning preprocessing
π Used with Other Libraries
Works seamlessly with:
- Pandas
- Scikit-learn
- TensorFlow
π Summary Table
| Operation | Example |
|---|---|
| Condition | arr > 25 |
| Filter | arr[arr > 25] |
| AND | (arr > 20) & (arr < 50) |
| Modify | arr[arr > 25] = 0 |
π§ Pro Tips
- Always use parentheses in multiple conditions
- Use boolean indexing instead of loops
- Combine with slicing for advanced queries
π Conclusion
Boolean indexing in NumPy is a must-know skill for efficient data filtering and manipulation.