Master NumPy From Scratch

Learn NumPy fundamentals, arrays, indexing, slicing, data types, array operations, broadcasting, mathematical functions, statistics, random numbers, linear algebra, file handling, and practical projects through structured lessons.

COURSE SYLLABUS
1. NumPy Introduction and Installation
Introduction to NumPy NumPy Installation and Setup NumPy Environment and IDE Setup First NumPy Program
2. NumPy Arrays and Array Creation
NumPy Array Fundamentals Array Creation Functions Zeros, Ones and Identity Arrays Range and Linspace Arrays
3. NumPy Array Indexing and Slicing
One-Dimensional Array Indexing Array Slicing Techniques Two-Dimensional Array Indexing Boolean and Fancy Indexing
4. NumPy Data Types and Attributes
NumPy Data Types Array Shape, Size and Dimensions Data Type Conversion and Casting Array Inspection and Attributes
5. NumPy Array Shape and Reshaping
Reshape and Flatten Arrays Transpose and Axis Operations Stacking and Splitting Arrays Expand and Squeeze Dimensions
6. NumPy Array Operations and Broadcasting
Arithmetic Array Operations Comparison and Logical Operations Broadcasting Fundamentals Universal Functions and Vectorization
7. NumPy Mathematical and Statistical Functions
Mathematical Functions Aggregate and Statistical Functions Minimum, Maximum and Percentiles Rounding and Trigonometric Functions
8. NumPy Filtering and Sorting
Array Filtering and Conditions Sorting and Searching Arrays Unique Values and Set Operations Where, Select and Nonzero Functions
9. NumPy Copies and Views
Array Copies and Views Shallow and Deep Copy Concepts Memory Layout and Contiguity Array Iteration and Memory Efficiency
10. NumPy Random Number Generation
Random Generator Fundamentals Random Integers and Sampling Probability Distributions Random Seeds and Simulation
11. NumPy Linear Algebra
Vectors and Matrices Matrix Multiplication and Dot Product Determinants and Matrix Inverse Eigenvalues and Linear Equations
12. NumPy File Input and Output
Importing and Exporting Text Files CSV Data with NumPy Binary Files and NPY Format NPZ Files and Data Persistence
13. NumPy Practical Projects
Student Marks Analysis Project Sales Data Analysis Project Random Simulation Project Image Array Processing Project
14. NumPy Practice and Assessment
NumPy Beginner Practice NumPy Intermediate Exercises NumPy Advanced Challenges Final NumPy Assessment

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.
Beginner tip: Boolean indexing selects elements according to True and False conditions, while fancy indexing selects elements using specified index positions.

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.

import numpy as np marks = np.array([65, 82, 90, 74, 88, 55]) result = marks > 80 print(result) print(marks[result])

Output

[False True True False True False] [82 90 88]

The comparison creates a Boolean mask. NumPy then uses that mask to return only the marks greater than 80.

Boolean Indexing Index 0 Index 1 Index 2 Index 3 Index 4 Index 5 65 82 90 74 88 55 Condition: marks > 80 False True True False True False Selected values: [82, 90, 88]
Figure 1: Boolean indexing selects values where the condition evaluates to True.

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.

values = np.array([10, 25, 30, 45, 50]) print(values > 30) # Output: [False False False True True] print(values == 30) # Output: [False False True False False] print(values < 25) # Output: [ True False False False False] print(values != 45) # Output: [ True True True False True]

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
Remember: Use == 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.

sales = np.array([250, 600, 450, 800, 520, 300]) high_sales = sales[sales >= 500] print(high_sales) # Output: [600 800 520]

The expression sales >= 500 generates the mask, and sales[mask] returns the matching values.

Filtering with Different Conditions

numbers = np.array([5, 12, 18, 23, 30, 41, 50]) # Select values greater than 20 print(numbers[numbers > 20]) # Output: [23 30 41 50] # Select even values print(numbers[numbers % 2 == 0]) # Output: [12 18 30 50] # Select values divisible by 5 print(numbers[numbers % 5 == 0]) # Output: [ 5 30 50]
Practical use: Boolean filtering is useful for selecting passing marks, high-value sales, eligible records, and measurements within a required range.

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.

numbers = np.array([10, 15, 20, 25, 30, 35, 40]) # Values greater than 15 and less than 35 result = numbers[(numbers > 15) & (numbers < 35)] print(result) # Output: [20 25 30] # Values less than 15 or greater than 35 result = numbers[(numbers < 15) | (numbers > 35)] print(result) # Output: [10 40] # Values that are not greater than 25 result = numbers[~(numbers > 25)] print(result) # Output: [10 15 20 25]

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)
Important: Put parentheses around each comparison when combining conditions. Use NumPy's & 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.

marks = np.array([ [78, 85, 90], [55, 92, 76], [88, 67, 95] ]) # Select every mark greater than 80 result = marks[marks > 80] print(result) # Output: [85 90 92 88 95]

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

marks = np.array([ [78, 85, 90], [55, 92, 76], [88, 67, 95] ]) # Increase every mark below 60 to 60 marks[marks < 60] = 60 print(marks) # Output: # [[78 85 90] # [60 92 76] # [88 67 95]]
Remember: Boolean assignment changes the elements selected by the condition in the original array.

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

numbers = np.array([10, 20, 30, 40, 50, 60]) result = numbers[[0, 2, 5]] print(result) # Output: [10 30 60]

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.

Fancy Indexing Index 0 Index 1 Index 2 Index 3 Index 4 Index 5 10 30 60 20 40 50 Requested indexes: [0, 2, 5] Result: [10, 30, 60]
Figure 2: Fancy indexing retrieves elements at explicitly specified positions.

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.

values = np.array([100, 200, 300, 400, 500, 600]) # Select indexes in the specified order print(values[[4, 1, 3]]) # Output: [500 200 400] # Repeat an index print(values[[2, 2, 0]]) # Output: [300 300 100] # Use a NumPy array of indexes indexes = np.array([0, 3, 5]) print(values[indexes]) # Output: [100 400 600]

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.
Remember: Fancy indexing uses a list or array of indexes inside the brackets, such as 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

data = np.array([ [10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120] ]) # Select rows at indexes 0 and 2 print(data[[0, 2]]) # Output: # [[10 20 30] # [70 80 90]]

Selecting Specific Columns

# Select columns at indexes 0 and 2 print(data[:, [0, 2]]) # Output: # [[ 10 30] # [ 40 60] # [ 70 90] # [100 120]]

Selecting Specific Row-Column Pairs

# Select positions (0, 1), (1, 2), and (2, 0) result = data[[0, 1, 2], [1, 2, 0]] print(result) # Output: [20 60 70]
Important: When two integer index arrays are used together, NumPy pairs their corresponding entries. Therefore, 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.

numbers = np.array([10, 20, 30, 40, 50, 60]) # Replace values at indexes 1 and 4 numbers[[1, 4]] = 99 print(numbers) # Output: [10 99 30 40 99 60]

Assigning Different Values

numbers = np.array([10, 20, 30, 40, 50]) numbers[[0, 2, 4]] = [100, 300, 500] print(numbers) # Output: [100 20 300 40 500]

Updating Values with a Boolean Condition

sales = np.array([200, 450, 150, 700, 300]) # Increase every value below 250 to 250 sales[sales < 250] = 250 print(sales) # Output: [250 450 250 700 300]
Practical use: Use Boolean indexing when values should be updated according to a condition. Use fancy indexing when the positions to update are already known.

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.
Debugging tip: Use array.shape, array.size, and array.dtype to inspect the array before applying advanced indexing.
values = np.array([10, 20, 30, 40, 50]) mask = values > 25 print(values.shape) # Output: (5,) print(mask.shape) # Output: (5,) print(values[mask]) # Output: [30 40 50]

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.

import numpy as np marks = np.array([72, 85, 91, 68, 88, 76, 95]) # Find marks greater than 80 high_marks = marks[marks > 80] print("High marks:", high_marks) # Find marks between 70 and 90 inclusive selected_marks = marks[(marks >= 70) & (marks <= 90)] print("Marks from 70 to 90:", selected_marks) # Retrieve values at selected indexes chosen_marks = marks[[0, 2, 4]] print("Selected positions:", chosen_marks) # Update marks below 70 marks[marks < 70] = 70 print("Updated marks:", marks)

Expected Output

High marks: [85 91 88 95] Marks from 70 to 90: [72 85 88 76] Selected positions: [72 91 88] Updated marks: [72 85 91 70 88 76 95]

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:

import numpy as np sales = np.array([120, 250, 180, 400, 320, 150, 500])
  1. Select all sales values greater than 200.
  2. Select all sales values less than or equal to 180.
  3. Select sales values between 150 and 400 inclusive.
  4. Select all even values from the array.
  5. Use fancy indexing to retrieve values at indexes 0, 3, and 6.
  6. Retrieve values at indexes 4, 1, and 5 in that order.
  7. Replace every sales value below 200 with 200.
  8. 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:

sales_data = np.array([ [120, 250, 300], [180, 400, 220], [350, 150, 500], [280, 320, 190] ])
  • 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.
Practice goal: Try solving each exercise without looking at the previous examples. Then check the results by printing the selected arrays.

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.