什么是计算函数最快的方法
# here x is just a number
def f(x):
if x >= 0:
return np.log(x+1)
else:
return -np.log(-x+1)
Run Code Online (Sandbox Code Playgroud)
一种可能的方法是:
# here x is an array
def loga(x)
cond = [x >= 0, x < 0]
choice = [np.log(x+1), -np.log(-x+1)
return np.select(cond, choice)
Run Code Online (Sandbox Code Playgroud)
但似乎numpy逐个元素地遍历数组.有没有办法使用概念上类似于np.exp(x)的东西来获得更好的性能?