How to Map a Function Over NumPy Array? Last Updated : 03 Jun, 2025 Comments Improve Suggest changes Like Article Like Report 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 operationThis 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) Output[3 4 5 6 7] 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 functionYou 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) Output[3 4 5 6 7] 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) Output[3 4 5 6 7] 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. Comment More infoAdvertise with us Next Article How to Map a Function Over NumPy Array? P pulamolusaimohan Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python 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? The tf.convert_to_tensor() method from the TensorFlow library is used to convert a NumPy array into a Tensor. The distinction between a NumPy array and a tensor is that tensors, unlike NumPy arrays, are supported by accelerator memory such as the GPU, they have a faster processing speed. there are a 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 Convert a NumPy array to an image Converting a NumPy array to an image is a simple way to turn numbers into pictures. A NumPy array holds pixel values, which are just numbers that represent colors. Images, like PNG or JPEG, store these pixel values in a format we can see. In this process, the NumPy array turns into an image, with ea 3 min read numpy.ma.append() function | Python numpy.ma.append() function append the values to the end of an array. Syntax : numpy.ma.append(arr1, arr2, axis = None) Parameters : arr1 : [array_like] Values are appended to a copy of this array. arr2 : [array_like] Values are appended to a copy of this array. If axis is not specified, arr2 can be 2 min read How to access a NumPy array by column Accessing a NumPy-based array by a specific Column index can be achieved by indexing. NumPy follows standard 0-based indexing in Python.  Example:Given array: 1 13 6 9 4 7 19 16 2 Input: print(NumPy_array_name[ :,2]) Output: [6 7 2] Explanation: printing 3rd columnAccess ith column of a 2D Numpy Arr 3 min read How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi 3 min read How to Change a Single Value in a NumPy Array NumPy arrays are a fundamental data structure in Python, widely used for scientific computing and data analysis. They offer a powerful way to perform operations on large datasets efficiently. One common task when working with NumPy arrays is changing a single value within the array. This article wil 6 min read Like