Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Array Filtering Explained – Boolean Indexing with Examples in Python

NumPy Array Filtering

In NumPy, filtering allows you to extract specific elements from an array based on conditions.

Instead of manually looping through data, NumPy lets you filter arrays using simple expressions.

This is called Array Filtering.


What is Array Filtering in NumPy?

Array filtering means:

Selecting elements from an array that satisfy a condition.

It is done using Boolean indexing.


Why Use Array Filtering?

  • Fast data selection
  • No loops required
  • Clean and readable code
  • Essential for data science
  • Used in machine learning preprocessing

1. Basic Filtering Example

import numpy as np

arr = np.array([10, 20, 30, 40, 50])

filtered = arr[arr > 25]

print(filtered)

Output:

[30 40 50]

2. How Filtering Works

arr = [10, 20, 30, 40, 50]
condition: > 25

Result:
[False False True True True]

Filtered values:
[30 40 50]

3. Filtering Even Numbers

arr = np.array([1, 2, 3, 4, 5, 6])

even = arr[arr % 2 == 0]

print(even)

Output:

[2 4 6]

4. Filtering Odd Numbers

arr = np.array([1, 2, 3, 4, 5, 6])

odd = arr[arr % 2 != 0]

print(odd)

Output:

[1 3 5]

5. Filtering with Multiple Conditions

arr = np.array([10, 15, 20, 25, 30, 35])

result = arr[(arr > 15) & (arr < 35)]

print(result)

Output:

[20 25 30]

Logical Operators in Filtering

OperatorMeaning
&        AND
|        OR
~        NOT

6. Filtering Using Comparison Arrays

arr = np.array([5, 10, 15, 20])

mask = arr > 10

print(arr[mask])

Output:

[15 20]

7. Filtering 2D Arrays

arr = np.array([
[10, 20, 30],
[40, 50, 60]
])

filtered = arr[arr > 30]

print(filtered)

Output:

[40 50 60]

8. Real-World Example: Student Scores

scores = np.array([45, 60, 75, 30, 90])

passed = scores[scores >= 50]

print(passed)

Output:

[60 75 90]

9. Real-World Example: Data Cleaning

data = np.array([100, -5, 50, -10, 200])

cleaned = data[data > 0]

print(cleaned)

Output:

[100  50 200]

10. Filtering vs Loop Method

❌ Using Loop:

result = []
for x in arr:
if x > 20:
result.append(x)

✅ Using NumPy Filtering:

result = arr[arr > 20]

✔ Faster
✔ Cleaner
✔ More efficient


Advantages of NumPy Filtering

  • High performance
  • Simple syntax
  • No loops needed
  • Works on large datasets
  • Essential for AI/ML workflows

Summary

NumPy filtering allows you to extract data based on conditions using boolean indexing.

It is a powerful feature in NumPy and widely used in data analysis and machine learning with Python.


Conclusion

Mastering array filtering helps you efficiently process, clean, and analyze data in Python, making your code faster and more powerful.




Post a Comment

0 Comments