条件函数的快速计算方法

2024-04-19 00:07:25 发布

您现在位置:Python中文网/ 问答频道 /正文

计算函数最快的方法是什么

# here x is just a number
def f(x):
    if x >= 0:
        return np.log(x+1)
    else:
        return -np.log(-x+1)

一种可能的方法是:

^{pr2}$

但似乎numpy通过数组元素一个元素。 有什么方法可以在概念上使用类似于np.exp.公司(x) 为了获得更好的表现?在


Tags: 方法函数numpylog元素numberreturnif
3条回答

在这种情况下,^{}有助于-

def mask_vectorized_app(x):
    out = np.empty_like(x)
    mask = x>=0
    mask_rev = ~mask
    out[mask] = np.log(x[mask]+1)
    out[mask_rev] = -np.log(-x[mask_rev]+1)
    return out

引入^{} module可以进一步帮助我们。在

^{pr2}$

^{}的启发,然后使用对数属性:log(A**B) = B*log(A),我们可以将符号推入到日志计算中,这使得我们可以对numexpr的求值表达式做更多的工作,如-

^{3}$

使用比较计算sign可以从另一个角度给我们s

s = (-2*(x<0))+1

最后,我们可以将其放入numexpr求值表达式-

def mask_vectorized_numexpr_app2(x):
    return ne.evaluate('log( (abs(x)+1)**((-2*(x<0))+1))')

运行时测试

比较的循环方法-

def loopy_app(x):
    out = np.empty_like(x)
    for i in range(len(out)):
        out[i] = f(x[i])
    return out

时间安排和验证-

In [141]: x = np.random.randn(100000)
     ...: print np.allclose(loopy_app(x), mask_vectorized_app(x))
     ...: print np.allclose(loopy_app(x), mask_vectorized_numexpr_app(x))
     ...: print np.allclose(loopy_app(x), mask_vectorized_numexpr_app2(x))
     ...: 
True
True
True

In [142]: %timeit loopy_app(x)
     ...: %timeit mask_vectorized_numexpr_app(x)
     ...: %timeit mask_vectorized_numexpr_app2(x)
     ...: 
10 loops, best of 3: 108 ms per loop
100 loops, best of 3: 3.6 ms per loop
1000 loops, best of 3: 942 µs per loop

使用^{}使用np.sign替换第一部分,然后使用和不使用{}求值-

^{8}$

Using ^{}

Numba gives you the power to speed up your applications with high performance functions written directly in Python. With a few annotations, array-oriented and math-heavy Python code can be just-in-time compiled to native machine instructions, similar in performance to C, C++ and Fortran, without having to switch languages or Python interpreters.

Numba works by generating optimized machine code using the LLVM compiler infrastructure at import time, runtime, or statically (using the included pycc tool). Numba supports compilation of Python to run on either CPU or GPU hardware, and is designed to integrate with the Python scientific software stack.

The Numba project is supported by Continuum Analytics and The Gordon and Betty Moore Foundation (Grant GBMF5423).

from numba import njit
import numpy as np

@njit
def pir(x):
    a = np.empty_like(x)
    for i in range(a.size):
        x_ = x[i]
        _x = abs(x_)
        a[i] = np.sign(x_) * np.log(1 + _x)
    return a

准确度

^{pr2}$

计时

^{3}$

函数定义

from numba import njit
import numpy as np

@njit
def pir(x):
    a = np.empty_like(x)
    for i in range(a.size):
        x_ = x[i]
        _x = abs(x_)
        a[i] = np.sign(x_) * np.log(1 + _x)
    return a

import numexpr as ne

def mask_vectorized_numexpr_app(x):
    out = np.empty_like(x)
    mask = x>=0
    mask_rev = ~mask

    x_masked = x[mask]
    x_rev_masked = x[mask_rev]
    out[mask] = ne.evaluate('log(x_masked+1)')
    out[mask_rev] = ne.evaluate('-log(-x_rev_masked+1)')
    return out

def mask_vectorized_numexpr_app2(x):
    return ne.evaluate('log( (abs(x)+1)**((-2*(x<0))+1))')


def f(x):
    return (x/abs(x)) * np.log(1+abs(x))
    def f(x):
        return (x/abs(x)) * np.log(1+abs(x))

相关问题 更多 >