Most efficient way to map function over numpy array

前端 未结 11 1340
庸人自扰
庸人自扰 2020-11-22 02:13

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          


        
11条回答
  •  花落未央
    2020-11-22 02:35

    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.

提交回复
热议问题