๐ Step-by-step explanation
-
a = np.array([1, 2, 3, 4])
a = [1, 2, 3, 4]
Creates a NumPy array: -
[True, False, True, False]
→ This is a boolean mask.
Each boolean value corresponds to whether to select (True) or ignore (False) the element at that index. -
Boolean indexing:
NumPy uses the boolean mask to filter elements:-
Index 0 → True → select 1
-
Index 1 → False → skip 2
-
Index 2 → True → select 3
-
Index 3 → False → skip 4
-
-
Result:
[1, 3]
✅ Output:
[1 3]๐ง Concept:
This is called Boolean Indexing in NumPy — a powerful feature that lets you filter arrays based on conditions.
For example:
Output:
[30 40]
๐ NumPy automatically creates a boolean mask [False, False, True, True] and returns elements where the condition is True.
.png)

0 Comments:
Post a Comment