# Filter NumPy Array with Lambda Functions

In this tutorial, you’ll learn how to filter NumPy with lambda functions.

Lambda functions in Python are small anonymous functions defined with the keyword `lambda`

.

We’ll explore various methods such as using `numpy.vectorize()`

with lambda functions, list comprehension, boolean indexing, and the `filter()`

function.

## Using numpy.where()

You can apply `numpy.where()`

with a lambda function to filter data:

import numpy as np data_usage = np.array([1.5, 4.8, 3.1, 0.9, 5.7, 2.3, 3.8]) heavy_users = np.where(lambda x: x > 3, data_usage, False) print(heavy_users)

Output:

[array([4.8, 5.7, 3.8])]

The lambda function `lambda x: x > 3`

acts as a filter criterion, selecting only those elements in `data_usage`

that satisfy this condition.

## Using numpy.vectorize()

`numpy.vectorize()`

transforms a Python function into a vectorized function that can operate on NumPy arrays.

When combined with lambda functions, `numpy.vectorize()`

provides a way to apply operations element-wise on arrays.

Suppose you want to categorize these scores into ‘Low’, ‘Medium’, and ‘High’.

Here’s how `numpy.vectorize()`

with a lambda function can be used for this purpose:

import numpy as np satisfaction_scores = np.array([7, 4, 9, 6, 3, 8, 5]) categorize = lambda x: 'High' if x > 6 else ('Medium' if x > 3 else 'Low') vectorized_categorize = np.vectorize(categorize) categories = vectorized_categorize(satisfaction_scores) print(categories)

Output:

['High' 'Medium' 'High' 'Medium' 'Low' 'High' 'Medium']

`numpy.vectorize(categorize)`

transforms this lambda function into a vectorized function that can be applied to the entire `satisfaction_scores`

array.

## List Comprehension with a Lambda Function

This method is useful in cases where you need to apply a simple function to each element in a list or array.

Here’s how you can do this using list comprehension with a lambda function:

monthly_expenses = [50, 75, 30, 45, 60] apply_discount = lambda x: x * 0.9 discounted_expenses = [apply_discount(expense) for expense in monthly_expenses] print(discounted_expenses)

Output:

[45.0, 67.5, 27.0, 40.5, 54.0]

In this example, the lambda function `apply_discount`

calculates 90% of the original expense, effectively applying a 10% discount.

The list comprehension `[apply_discount(expense) for expense in monthly_expenses]`

iterates over `monthly_expenses`

, applying the discount to each element.

## Boolean Indexing with a Lambda Function

Boolean indexing in NumPy allows for the selection of elements based on specific conditions.

Here’s how you can use boolean indexing and a lambda function:

import numpy as np customer_ages = np.array([25, 67, 50, 72, 34, 58, 62]) is_senior = lambda x: x >= 60 senior_citizens = customer_ages[is_senior(customer_ages)] print(senior_citizens)

Output:

[67 72 62]

This code uses the lambda function `is_senior`

to create a boolean array, where each element in `customer_ages`

is checked against the condition `x >= 60`

.

This boolean array is then used to index `customer_ages`

, resulting in an array `senior_citizens`

that contains only the ages of customers who are 60 years or older.

## Using filter() Function

The `filter()`

function in Python is used to construct an iterator from elements of an iterable for which a function returns true.

When `filter()`

function combined with a lambda function, it allows you to filter data based on custom criteria.

Here’s how you can use the `filter()`

function with a lambda:

import numpy as np call_durations = np.array([3, 12, 1, 9, 5, 2, 8]) effective_call = lambda x: x >= 5 effective_calls = list(filter(effective_call, call_durations)) print(effective_calls)

Output:

[12, 9, 5, 8]

In this code, the lambda function `effective_call`

defines the criteria for filtering: `x >= 5`

.

The `filter()`

function then applies this criterion to each element in `call_durations`

.

The result is an iterator, which we convert into a list `effective_calls`

.

Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.