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

NumPy Array Filtering and Conditions

Array filtering allows you to select values from a NumPy array based on specified conditions. You can identify values greater than a limit, select marks within a range, filter numerical measurements, and combine multiple conditions. In this lesson, you will learn Boolean masks, conditional selection, logical operators, and practical filtering techniques for numerical datasets.

1. Introduction to Array Filtering

Array filtering means selecting only the elements that satisfy a condition. NumPy comparisons produce Boolean arrays containing True or False values. These Boolean arrays can then be used to select matching elements.

import numpy as np marks = np.array([45, 67, 82, 39, 91, 74]) condition = marks >= 70 print(condition) print(marks[condition])

Output:

[False False True False True True] [82 91 74]

The condition checks whether each mark is at least 70. Only the values whose conditions are True are selected.

Important Concepts

  • Condition: A comparison that evaluates to True or False.
  • Boolean mask: An array of Boolean values used for selection.
  • Filtered result: The values that satisfy the condition.
  • Boolean indexing: Using a Boolean mask to select array elements.

2. Filtering with Comparison Operators

Comparison operators allow you to build conditions based on numeric values. They work element by element on NumPy arrays.

Operator Meaning Example
> Greater than values > 50
< Less than values < 50
>= Greater than or equal to values >= 50
<= Less than or equal to values <= 50
== Equal to values == 50
!= Not equal to values != 50
import numpy as np values = np.array([10, 25, 50, 75, 100]) print("Greater than 50:", values[values > 50]) print("Less than 50:", values[values < 50]) print("Equal to 50:", values[values == 50]) print("Not equal to 50:", values[values != 50])

Output:

Greater than 50: [ 75 100] Less than 50: [10 25] Equal to 50: [50] Not equal to 50: [ 10 25 75 100]

3. Creating and Using Boolean Masks

A Boolean mask stores the result of a condition separately. You can reuse the mask to select values or count how many elements satisfy the condition.

import numpy as np temperatures = np.array([18, 24, 31, 27, 15, 35]) warm_mask = temperatures >= 25 print("Boolean mask:", warm_mask) print("Warm temperatures:", temperatures[warm_mask]) print("Number of warm readings:", np.count_nonzero(warm_mask))

Output:

Boolean mask: [False False True True False True] Warm temperatures: [31 27 35] Number of warm readings: 3
Boolean Mask: Select Values Greater Than or Equal to 25 Values 18 24 31 27 15 35 Mask False False True True False True Filtered values: [31, 27, 35]

Only elements corresponding to True values in the mask are selected.

4. Combining Multiple Conditions

You can combine conditions to filter values using logical operators. For NumPy arrays, use & for AND, | for OR, and ~ for NOT.

import numpy as np marks = np.array([35, 48, 56, 67, 74, 88, 95]) passed_and_below_90 = marks[(marks >= 40) & (marks < 90)] high_or_low = marks[(marks >= 85) | (marks < 40)] not_passed = marks[~(marks >= 40)] print("Passed and below 90:", passed_and_below_90) print("High or low marks:", high_or_low) print("Below passing mark:", not_passed)

Output:

Passed and below 90: [48 56 67 74 88] High or low marks: [35 88 95] Below passing mark: [35]
Important: Put parentheses around each comparison when combining conditions. Use & and | instead of Python's and and or for element-wise array conditions.

5. Filtering Values Within a Range

Range filtering selects values that fall between a lower and an upper boundary. The boundary conditions determine whether the endpoints are included.

import numpy as np prices = np.array([50, 120, 180, 250, 320, 450, 600]) selected_prices = prices[(prices >= 150) & (prices <= 450)] print("All prices:", prices) print("Selected prices:", selected_prices)

Output:

All prices: [ 50 120 180 250 320 450 600] Selected prices: [180 250 320 450]

Exclusive Boundaries

values = np.array([10, 20, 30, 40, 50]) result = values[(values > 20) & (values < 50)] print(result)

Output:

[30 40]

The first example includes both endpoints because it uses >= and <=. The second example excludes the endpoints because it uses strict comparisons.

6. Filtering with np.where()

The np.where() function can return indices that satisfy a condition. It can also choose values from two alternatives depending on a condition.

Return Matching Indices

import numpy as np values = np.array([12, 45, 67, 23, 89, 34]) indices = np.where(values > 50) print(indices) print("Matching values:", values[indices])

Output:

(array([2, 4]),) Matching values: [67 89]

Choose Between Two Values

import numpy as np marks = np.array([35, 55, 78, 29, 91]) status = np.where(marks >= 40, "Pass", "Fail") print(status)

Output:

['Fail' 'Pass' 'Pass' 'Fail' 'Pass']
Tip: Use Boolean indexing when you need only the matching values. Use np.where() when you need matching indices or conditional selection between alternative values.

7. Filtering Two-Dimensional Arrays

Boolean conditions can also be applied to matrices. When you use a Boolean condition over the entire matrix, the result is a one-dimensional array containing all matching elements in row-major order.

import numpy as np marks = np.array([ [65, 82, 45], [91, 38, 76], [55, 88, 94] ]) high_marks = marks[marks >= 80] print("Marks matrix:") print(marks) print("Marks >= 80:") print(high_marks)

Output:

Marks matrix: [[65 82 45] [91 38 76] [55 88 94]] Marks >= 80: [82 91 88 94]

Filter Complete Rows

If you want to keep entire rows rather than individual elements, create a condition for each row.

import numpy as np marks = np.array([ [65, 82, 75], [91, 88, 94], [55, 68, 72] ]) row_averages = np.mean(marks, axis=1) selected_rows = marks[row_averages >= 80] print("Row averages:", row_averages) print("Selected rows:") print(selected_rows)

Output:

Row averages: [74. 91. 65. ] Selected rows: [[91 88 94]]

The Boolean condition is evaluated once for each row average, so the selected result retains the complete rows that satisfy the condition.

8. Counting and Checking Conditions

NumPy provides functions to count matching values and determine whether some or all elements satisfy a condition.

import numpy as np values = np.array([12, 25, 37, 48, 59]) condition = values > 30 print("Matching count:", np.count_nonzero(condition)) print("Any value above 50:", np.any(values > 50)) print("All values positive:", np.all(values > 0)) print("All values above 20:", np.all(values > 20))

Output:

Matching count: 3 Any value above 50: True All values positive: True All values above 20: False
Function Purpose
np.count_nonzero(condition) Counts nonzero values in the condition array.
np.any(condition) Returns True if at least one element satisfies the condition.
np.all(condition) Returns True if every element satisfies the condition.

9. Filtering NaN and Infinite Values

Numerical arrays can contain missing values represented by np.nan or infinite values represented by np.inf and -np.inf. NumPy provides functions to identify finite values and NaN values.

import numpy as np values = np.array([10.0, np.nan, 25.0, np.inf, -np.inf, 40.0]) print("NaN mask:", np.isnan(values)) print("Finite mask:", np.isfinite(values)) print("Finite values:", values[np.isfinite(values)])

Output:

NaN mask: [False True False False False False] Finite mask: [ True False True False False True] Finite values: [10. 25. 40.]
Important: A comparison such as values > 0 does not reliably identify valid finite measurements. Use np.isfinite() when you specifically need to exclude NaN and positive or negative infinity.

10. Practical Project: Student Marks Filtering

In this project, we will filter students according to their marks, identify high scorers, and determine pass or fail status.

import numpy as np students = np.array(["Samadhan", "Pradnya", "Samiksha"]) marks = np.array([68, 92, 37]) passing_mask = marks >= 40 high_score_mask = marks >= 80 print("Passing students:", students[passing_mask]) print("Passing marks:", marks[passing_mask]) print("High scorers:", students[high_score_mask]) status = np.where(marks >= 40, "Pass", "Fail") for name, mark, result in zip(students, marks, status): print(name, "- Marks:", mark, "- Status:", result)

Output:

Passing students: ['Samadhan' 'Pradnya'] Passing marks: [68 92] High scorers: ['Pradnya'] Samadhan - Marks: 68 - Status: Pass Pradnya - Marks: 92 - Status: Pass Samiksha - Marks: 37 - Status: Fail

Project Learning Outcomes

  • Use a Boolean mask to select students who passed.
  • Identify students whose marks meet a high-score threshold.
  • Use np.where() to assign conditional status labels.
  • Keep student names and marks aligned using the same mask.

11. Common Mistakes and Best Practices

Using and/or Instead of Array Operators

Use & and | for element-wise Boolean operations on NumPy arrays. The Python keywords and and or do not work as element-wise array operators.

Forgetting Parentheses

Write conditions such as (values > 10) & (values < 50). Parentheses ensure each comparison is evaluated as intended.

Confusing Element Filtering with Row Filtering

Applying a mask directly to a matrix selects matching elements. To retain complete rows, create a Boolean condition with one value per row.

Using the Wrong Comparison Boundary

Use >= or <= when endpoints should be included. Use > or < when endpoints should be excluded.

Ignoring Invalid Numerical Values

Use functions such as np.isnan() and np.isfinite() when missing or infinite values need special handling.

12. Practice Exercises

Exercise 1: Basic Filtering

Create an array containing [12, 45, 67, 23, 89, 34]. Select all values greater than 40.

Exercise 2: Range Filtering

Select values between 20 and 60 from a numerical array. Try both inclusive and exclusive boundaries.

Exercise 3: Combine Conditions

Select values that are greater than 25 and less than 75 using a combined Boolean condition.

Exercise 4: Use np.where()

Create an array of marks and assign Pass when marks are at least 40 and Fail otherwise.

Exercise 5: Matrix Filtering

Create a 3 by 3 matrix. Select all values greater than 70, then filter complete rows whose average is greater than 70.

Exercise 6: Valid Measurements

Create an array containing ordinary numbers, NaN, and infinity. Use np.isfinite() to select only finite values.

Summary

In this lesson, you learned to filter NumPy arrays using comparison operators and Boolean masks. You practiced selecting values with single conditions, combining conditions with &, |, and ~, and filtering values within a specified range.

You also explored np.where(), matrix and row filtering, condition counting with np.count_nonzero(), np.any(), and np.all(), and handling missing or infinite values using np.isnan() and np.isfinite().