Most efficient way to map function over numpy array

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

    How about using numpy.vectorize.

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

提交回复
热议问题