Most efficient way to map function over numpy array

前端 未结 11 1329
庸人自扰
庸人自扰 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:34

    I believe in newer version( I use 1.13) of numpy you can simply call the function by passing the numpy array to the fuction that you wrote for scalar type, it will automatically apply the function call to each element over the numpy array and return you another numpy array

    >>> import numpy as np
    >>> squarer = lambda t: t ** 2
    >>> x = np.array([1, 2, 3, 4, 5])
    >>> squarer(x)
    array([ 1,  4,  9, 16, 25])
    

提交回复
热议问题