NumPy apply() function: Explained with examples

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is an essential library in the Python data science stack. One of its key features is the ability to perform vectorized operations, which allow for the application of functions to arrays efficiently. This tutorial explores the concept of the ‘apply’ mechanism in NumPy and uses several examples to demonstrate its capabilities, from basic to advanced use cases.

Understanding Vectorization

Before diving into examples, let’s clarify what vectorization is. In NumPy, vectorization refers to the practice of applying an operation to every element in an array without the explicit use of for loops. This is not only more readable but also significantly faster because NumPy operations are implemented in C, benefitting from performance optimizations.

Basic Usage of NumPy ‘apply’

While NumPy does not have a direct apply() function like Pandas, the concept still exists in different forms. For example, np.vectorize() is one way to apply a function to each item in an array.

import numpy as np

def square(x):
    return x * x

# Vectorize the 'square' function
vec_square = np.vectorize(square)

# Apply it to an array
arr = np.array([1, 2, 3, 4])
print(vec_square(arr))
# Output: [ 1  4  9 16]

Element-wise Operations

Most of NumPy’s built-in functions support vectorized operations natively. We can perform element-wise operations without the need to wrap them using np.vectorize().

import numpy as np

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

# Add 10 to each element
result = arr + 10
print(result)
# Output: [11 12 13 14]

Applying Universal Functions (ufunc)

NumPy universal functions (ufuncs) are another set of functions that support vectorized operations. A ufunc operates on ndarrays in an element-by-element fashion. Let’s use np.sqrt() as an example:

import numpy as np

arr = np.array([1, 4, 9, 16])

# Apply square root to each element
result = np.sqrt(arr)
print(result)
# Output: [1. 2. 3. 4.]

Advanced Vectorization

For more advanced applications, you can use np.apply_along_axis(), which lets you apply a function across a specified axis of a multidimensional array.

import numpy as np

def my_func(a):
  return a[0] + a[-1]

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

# Apply 'my_func' across the first axis (rows)
result = np.apply_along_axis(my_func, 0, arr)
print(result)
# Output: [12 15 18]

Conclusion

To wrap it up, understanding how to apply functions to NumPy arrays is crucial for efficient data processing. We’ve explored some of the ways this can be achieved, from straightforward operations and universal functions to more sophisticated methods like np.apply_along_axis(). With these examples, you now have a firm grasp of applying logic across NumPy arrays and leveraging the library’s power.