How can I apply a function to each element of a NumPy array?

How can I apply a function to each element of a NumPy array?

One can apply a function to each element of a NumPy array by using the built-in function “apply_along_axis” or by using the “vectorize” function. Both of these functions allow for the application of a user-defined function to each element of a NumPy array, making it easier to perform operations on large arrays efficiently. Additionally, using these functions helps to avoid the need for writing loops, reducing the overall code complexity and increasing its readability.

Map a Function Over a NumPy Array (With Examples)


You can use the following basic syntax to map a function over a NumPy array:

#define function
my_function = lambda x: x*5

#map function to every element in NumPy array
my_function(my_array)

The following examples show how to use this syntax in practice.

Example 1: Map Function Over 1-Dimensional NumPy Array

The following code shows how to map a function to a NumPy array that multiplies each value by 2 and then adds 5:

import numpy as np

#create NumPy array
data = np.array([1, 3, 4, 4, 7, 8, 13, 15])

#define function
my_function = lambda x: x*2+5

#apply function to NumPy array
my_function(data)

array([ 7, 11, 13, 13, 19, 21, 31, 35])

Here is how each value in the new array was calculated:

  • First value: 1*2+5 = 7
  • Second value: 3*2+5 = 11
  • Third value: 4*2+5 = 13

And so on.

Example 2: Map Function Over Multi-Dimensional NumPy Array

The following code shows how to map a function to a multi-dimensional NumPy array that multiplies each value by 2 and then adds 5:

import numpy as np

#create NumPy array
data = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

#view NumPy array
print(data)

[[1 2 3 4]
 [5 6 7 8]]

#define function
my_function = lambda x: x*2+5

#apply function to NumPy array
my_function(data)

array([[ 7,  9, 11, 13],
       [15, 17, 19, 21]])

Notice that this syntax worked with a multi-dimensional array just as well as it worked with a one-dimensional array.

The following tutorials explain how to perform other common operations in NumPy:

Cite this article

stats writer (2024). How can I apply a function to each element of a NumPy array?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-each-element-of-a-numpy-array/

stats writer. "How can I apply a function to each element of a NumPy array?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-each-element-of-a-numpy-array/.

stats writer. "How can I apply a function to each element of a NumPy array?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-each-element-of-a-numpy-array/.

stats writer (2024) 'How can I apply a function to each element of a NumPy array?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-each-element-of-a-numpy-array/.

[1] stats writer, "How can I apply a function to each element of a NumPy array?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I apply a function to each element of a NumPy array?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top