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.
Output:
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 |
Output:
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.
Output:
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.
Output:
& 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.
Output:
Exclusive Boundaries
Output:
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
Output:
Choose Between Two Values
Output:
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.
Output:
Filter Complete Rows
If you want to keep entire rows rather than individual elements, create a condition for each row.
Output:
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.
Output:
| 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.
Output:
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.
Output:
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().