Boolean and Fancy Indexing
Learn how to select NumPy array elements using Boolean conditions and lists of indexes. Explore filtering, multiple conditions, advanced element selection, and updating selected values.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand Boolean indexing in NumPy.
- Create Boolean masks using comparison operators.
- Filter array elements using conditions.
- Combine conditions using logical operators.
- Use fancy indexing with lists and arrays of indexes.
- Select multiple elements in a specific order.
- Use fancy indexing with two-dimensional arrays.
- Modify selected array elements.
- Recognize common Boolean and fancy indexing errors.
- Apply indexing techniques to student marks and sales data.
2. What Is Boolean Indexing?
Boolean indexing is a NumPy technique used to select array elements
based on a condition. A condition produces Boolean values:
True or False.
When a Boolean mask is used to index an array, NumPy selects the
elements corresponding to True positions and excludes
those corresponding to False positions.
Example: Selecting Marks Greater Than 80
Samadhan wants to identify all marks greater than 80 from a student's marks array.
Output
The comparison creates a Boolean mask. NumPy then uses that mask to return only the marks greater than 80.
3. Creating Boolean Masks
A Boolean mask is an array containing True and False values. NumPy creates masks automatically when comparison operators are applied to arrays.
Common Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
> |
Greater than | values > 30 |
< |
Less than | values < 30 |
>= |
Greater than or equal to | values >= 30 |
<= |
Less than or equal to | values <= 30 |
== |
Equal to | values == 30 |
!= |
Not equal to | values != 30 |
== to compare values.
A single = is an assignment operator.
4. Filtering Arrays Using Boolean Indexing
You can apply a Boolean condition directly inside square brackets to filter an array. This is a convenient way to retrieve values without manually checking each element.
Example: Filtering Sales Values
Pradnya is analyzing daily sales and wants to find all sales values that are at least 500.
The expression sales >= 500 generates the mask,
and sales[mask] returns the matching values.
Filtering with Different Conditions
5. Combining Multiple Conditions
NumPy allows multiple conditions to be combined into one Boolean
mask. Use the bitwise operators &,
|, and ~ for element-wise logical
operations on Boolean arrays.
Logical Operators for Boolean Masks
| Operator | Meaning | Example |
|---|---|---|
& |
Both conditions must be True. | (a > 10) & (a < 30) |
| |
At least one condition must be True. | (a < 10) | (a > 30) |
~ |
Inverts Boolean values. | ~(a > 10) |
& and
| operators for element-wise conditions rather than
Python's and and or.
6. Boolean Indexing with Two-Dimensional Arrays
Boolean indexing also works with two-dimensional arrays. You can select individual values from a matrix using a condition that evaluates each element.
When a Boolean condition is applied directly to the complete matrix, the selected result is a one-dimensional array of matching elements.
Updating Elements Using a Condition
7. What Is Fancy Indexing?
Fancy indexing allows you to select multiple elements by providing a list or NumPy array of integer indexes. Unlike ordinary slicing, the indexes can be specified individually and in a chosen order.
Example: Selecting Specific Positions
The index list [0, 2, 5] selects the elements at
those three positions. The original array remains unchanged
when you only retrieve the selected values.
8. Fancy Indexing with Lists and Index Arrays
You can pass a Python list or a NumPy array containing integer indexes to select multiple elements. You can also repeat indexes or arrange them in any valid order.
Fancy Indexing vs. Slicing
| Feature | Slicing | Fancy Indexing |
|---|---|---|
| Example | values[1:5] |
values[[1, 3, 5]] |
| Selection | Usually selects a continuous range. | Selects specified positions. |
| Order | Follows the slice order and step. | Follows the supplied index order. |
| Repeated indexes | Does not explicitly repeat individual positions. | Can repeat indexes. |
| Retrieved result | Often a view for basic slicing. | An advanced-indexing selection is a copy. |
values[[0, 2, 4]].
9. Fancy Indexing in Two-Dimensional Arrays
For two-dimensional arrays, fancy indexing can select particular rows, particular columns, or specific row-column combinations.
Selecting Specific Rows
Selecting Specific Columns
Selecting Specific Row-Column Pairs
data[[0, 1], [1, 2]] selects
data[0, 1] and data[1, 2];
it does not select every combination of those rows and columns.
10. Modifying Values with Fancy Indexing
Fancy indexing can also be used on the left side of an assignment to update elements at selected positions.
Assigning Different Values
Updating Values with a Boolean Condition
11. Boolean and Fancy Indexing Comparison
| Aspect | Boolean Indexing | Fancy Indexing |
|---|---|---|
| Selection method | Uses True and False conditions. | Uses specified integer indexes. |
| Typical example | marks[marks > 80] |
marks[[0, 2, 4]] |
| Best suited for | Filtering values by rules. | Selecting known positions. |
| Selection count | Depends on how many elements satisfy the condition. | Depends on the supplied index array. |
| Result | Returns selected elements in a new array. | Returns selected elements in a new array. |
| Typical application | Find marks above a threshold. | Retrieve selected student records. |
Choosing the Right Technique
- Use Boolean indexing when you know the condition.
- Use fancy indexing when you know the index positions.
- Use both techniques together when a task requires filtering followed by position-based selection.
12. Common Boolean and Fancy Indexing Errors
| Error | Reason | Solution |
|---|---|---|
| Boolean mask length mismatch | The mask does not match the array dimension being indexed. | Check the mask and array shapes. |
Using and or or |
Python logical operators are not the element-wise operators for NumPy arrays. | Use & or | with parentheses. |
| Invalid integer index | The requested position is outside the array bounds. | Check valid indexes using the array size. |
| Using floats as indexes | Fancy indexing requires integer index values. | Provide an appropriate integer index array. |
| Unexpected paired selection | Two integer index arrays select corresponding coordinate pairs. | Use the correct row and column index arrays for the intended selection. |
| Incorrect assignment shape | The replacement values are incompatible with the selected elements. | Provide compatible values and shapes. |
array.shape,
array.size, and array.dtype to inspect
the array before applying advanced indexing.
13. Practical Activity: Student Marks Analysis
Activity: Filter and Select Student Marks
Samiksha is reviewing examination marks. Use Boolean indexing to find marks above 80 and fancy indexing to retrieve marks from selected positions.
Expected Output
This activity demonstrates filtering by conditions, combining Boolean conditions, retrieving specific positions, and updating values in the original array.
14. Practice Exercises
Use the following array to complete the exercises:
- Select all sales values greater than 200.
- Select all sales values less than or equal to 180.
- Select sales values between 150 and 400 inclusive.
- Select all even values from the array.
- Use fancy indexing to retrieve values at indexes 0, 3, and 6.
- Retrieve values at indexes 4, 1, and 5 in that order.
- Replace every sales value below 200 with 200.
- Print the shape of the Boolean mask created by
sales > 250.
Challenge Exercise
Pradnya has a two-dimensional array containing monthly sales for four products:
- Select all sales values greater than 300.
- Select every row containing a value greater than 400.
- Select rows at indexes 0 and 2 using fancy indexing.
- Select columns at indexes 0 and 2.
- Replace every sales value below 200 with 200.
- Use two integer index arrays to retrieve selected row-column pairs.
Summary
In this lesson, we learned how Boolean indexing selects elements using True and False conditions. We created Boolean masks with comparison operators and filtered arrays using single and combined conditions.
We also explored fancy indexing with lists and NumPy arrays of integer indexes. We used advanced indexing to select particular rows, columns, and row-column pairs in two-dimensional arrays.
Finally, we learned how to update selected elements, compare Boolean indexing with fancy indexing, and troubleshoot common indexing errors.
Boolean and fancy indexing are important tools for efficient data filtering, selection, and manipulation in NumPy.
In the next lesson, you will learn about NumPy data types and how to inspect and work with the data types stored in arrays.