13.8 C
New York
Monday, March 4, 2024

Masking In NumPy Aarrys | Every Data Scientist Should Know About It.

Masking In NumPy Aarrys | Every Data Scientist Should Know About It.

Every aspiring data scientist should know about this.

Masking in NumPy arrays is a powerful technique that enables users to manipulate, analyze, and filter data efficiently. Masking involves the use of boolean arrays to extract, modify, or operate on elements in NumPy arrays based on specified conditions. This tutorial will explore the fundamentals of masking in NumPy, showcasing how it can be used for various data manipulation tasks.

You can support me on Kofi or support me by clapping and sharing this article.

Masking in NumPy (Image by the author)

What is Masking in NumPy?
Masking refers to the process of creating boolean arrays (masks) based on specific criteria applied to elements within a NumPy array. These boolean masks act as filters, allowing users to select elements that satisfy particular conditions while disregarding others.

import numpy as np

# Creating a NumPy array
data = np.array([10, 20, 30, 40, 50])

# Generating a mask for elements greater than 30
mask = data > 30
print(mask) # Output: [False False False True True]

Applying Masks
Once a mask is created, it can be used to filter elements from the original array or perform specific operations selectively:

# Selecting elements greater than 30 using the mask
filtered_data = data[mask]
print(filtered_data) # Output: [40 50]

# Performing operations on selected elements
data[mask] = 0
print(data) # Output: [10 20 30 0 0]

In this example, filtered_data contains elements greater than 30 from the original array, and by applying the mask to the data array, elements greater than 30 are replaced with zeros.

Combining Masks
NumPy allows combining multiple conditions using logical operators (AND, OR, NOT) to create complex masks:

# Creating masks for multiple conditions
mask1 = data < 20
mask2 = data % 10 == 0

# Combining masks using logical operators
combined_mask = np.logical_and(mask1, mask2)
print(combined_mask) # Output: [False True False True True]

The combined_mask here select elements less than 20 AND divisible by 10.

Source link

Latest stories