gpt4 book ai didi

python - 快速计算条件函数的方法

转载 作者:太空狗 更新时间:2023-10-30 00:31:13 24 4
gpt4 key购买 nike

计算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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com