What is the most efficient way to map a function over a numpy array? The way I\'ve been doing it in my current project is as follows:
import numpy as np
x
Edit: the original answer was misleading, np.sqrt
was applied directly to the array, just with a small overhead.
In multidimensional cases where you want to apply a builtin function that operates on a 1d array, numpy.apply_along_axis is a good choice, also for more complex function compositions from numpy and scipy.
Previous misleading statement:
Adding the method:
def along_axis(x):
return np.apply_along_axis(f, 0, x)
to the perfplot code gives performance results close to np.sqrt
.