How to Map a Function Over NumPy Array?
Last Updated :
03 Jun, 2025
Mapping a function over a NumPy array means applying a specific operation to each element individually. This lets you transform all elements of the array efficiently without writing explicit loops. For example, if you want to add 2 to every element in an array [1, 2, 3, 4, 5], the result will be [3, 4, 5, 6, 7] after applying the addition function to each element.
Using vectorized operation
This uses NumPy's broadcasting feature, where scalar 2 is automatically applied to each element of the array. It uses highly optimized low-level C loops internally.
Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
res = arr + 2
print(res)
Explanation: operation arr + 2 uses NumPy's vectorized broadcasting to efficiently add 2 to each element without explicit loops.
Using np.add()
np.add() is a universal function ufunc provided by NumPy. It performs element-wise addition and is equivalent to arr + 2, but more explicit.
Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
res = np.add(arr, 2)
print(res)
Explanation: np.add(arr, 2) function performs element-wise addition by adding 2 to each element of the array.
Using lambda function
You can define an anonymous function using lambda and apply it to the entire NumPy array. NumPy allows broadcasting of such operations.
Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
res = (lambda x: x + 2)(arr)
print(res)
Explanation: Lambda function (lambda x: x + 2) is immediately called with arr as the argument. This function adds 2 to each element of the array using NumPy’s broadcasting.
Using numpy.vectorize()
np.vectorize() takes a regular Python function and returns a vectorized version of it. It applies the function element-by-element over a NumPy array.
Python
import numpy as np
a = np.array([1, 2, 3, 4, 5])
f = np.vectorize(lambda x: x + 2)
res = f(a)
print(res)
Explanation: np.vectorize() which takes a Python lambda function that adds 2 to its input. When we call f(a), this vectorized function applies the lambda element-wise to each item in the array.
Similar Reads
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
NumPy Array Functions NumPy array functions are a set of built-in operations provided by the NumPy library that allow users to perform various tasks on arrays. With NumPy array functions, you can create, reshape, slice, sort, perform mathematical operations, and much moreâall while taking advantage of the library's speed
3 min read
numpy matrix operations | eye() function numpy.matlib.eye() is another function for doing matrix operations in numpy. It returns a matrix with ones on the diagonal and zeros elsewhere. Syntax : numpy.matlib.eye(n, M=None, k=0, dtype='float', order='C') Parameters : n : [int] Number of rows in the output matrix. M : [int, optional] Number o
2 min read
How To Convert Numpy Array To Tensor? We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho
2 min read
How to Convert a Dataframe Column to Numpy Array NumPy and Pandas are two powerful libraries in the Python ecosystem for data manipulation and analysis. Converting a DataFrame column to a NumPy array is a common operation when you need to perform array-based operations on the data. In this section, we will explore various methods to achieve this t
2 min read