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

标签 python performance numpy

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

相关文章:

python - 安全删除内存中的密码 (Python)

python - matplotlib、pyplot : custom color for a specific data value

iOS UIScrollView 微调

javascript - 有没有办法知道 'platform'访问网页的硬件资源?

python - 将四边形和三角形的网格转换为仅由三角形组成的网格

python - 操作 numpy 数组

python - 为什么这个函数返回全零

python - 如何在生成的列表上使用 pytest 测试

python - itertools.groupby() 生成的迭代器被意外消耗

python - Numpy:为什么 (2,1) 数组和垂直矩阵切片的差异不是 (2,1) 数组