计算like函数最快的方法是什么
# here x is just a number
def f(x):
if x >= 0:
return np.log(x+1)
else:
return -np.log(-x+1)
一种可能的方法是:
# 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)
但似乎 numpy 逐个元素地遍历数组。 有没有什么方法可以使用概念上类似于 np.exp(x) 的东西来获得更好的性能?
最佳答案
def f(x):
return (x/abs(x)) * np.log(1+abs(x))
关于python - 快速计算条件函数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44276950/