0
0
NumPydata~5 mins

Boolean indexing for filtering in NumPy - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Boolean indexing for filtering
O(n)
Understanding Time Complexity

We want to understand how the time needed to filter data with boolean indexing changes as the data size grows.

How does the filtering step scale when we have more data?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import numpy as np

arr = np.arange(1000)
mask = arr % 2 == 0
filtered = arr[mask]

This code creates an array of numbers, makes a mask for even numbers, and filters the array using that mask.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Checking each element to create the boolean mask and then selecting elements based on that mask.
  • How many times: Once for each element in the array (n times).
How Execution Grows With Input

As the array size grows, the number of checks and selections grows proportionally.

Input Size (n)Approx. Operations
10About 10 checks and selections
100About 100 checks and selections
1000About 1000 checks and selections

Pattern observation: The work grows directly with the number of elements.

Final Time Complexity

Time Complexity: O(n)

This means the time to filter grows in a straight line as the data size increases.

Common Mistake

[X] Wrong: "Filtering with boolean indexing is instant no matter how big the array is."

[OK] Correct: The code must check each element to decide if it matches the condition, so more data means more work.

Interview Connect

Understanding how filtering scales helps you explain data processing steps clearly and shows you know how to handle bigger datasets efficiently.

Self-Check

"What if we used multiple conditions combined with & or | in the mask? How would the time complexity change?"