Piecewise Functions on Numpy Arrays

What is an efficient way (speed) for applying piecewise functions in a Numpy array?

Say for example piecewise functions are similar to

For (1) : x<=2 f(x) = 2*x + x^2 (2) : x>2 f(x) = -(x^2 + 2) 

Here is what I did.

 data = np.random.random_integers(5, size=(5,6)) print data np.piecewise(data, [data <= 2, data > 2], [lambda x: 2*x + pow(2, x), lambda x: -(pow(x, 2) + 2)]) data = [[4 2 1 1 5 3] [4 3 3 5 4 5] [3 2 4 2 5 3] [2 5 4 3 1 4] [5 3 3 5 5 5]] output = array([[-18, 8, 4, 4, -27, -11], [-18, -11, -11, -27, -18, -27], [-11, 8, -18, 8, -27, -11], [ 8, -27, -18, -11, 4, -18], [-27, -11, -11, -27, -27, -27]]) 

Is there an efficient method for smaller arrays, large arrays, lots of functions, etc.? I'm worried about using lambda functions. Not sure if they are optimized.

+5
source share
1 answer

In this case, you should not worry about lambdas: Numpy's optimization is to reduce the overhead of calls, allowing functions to simultaneously evaluate multiple values ​​in a package. In every np.piecewise call np.piecewise each function in the funclist (functional parts) is called exactly once, with a numpy array consisting of all values ​​where the corresponding condition is true. Thus, these lambdas are invoked in an optimized way.

Similar to np.select (and np.where exactly two parts). Call overhead is the same as vectorized, but they will evaluate all the functions for all data points. Thus, it will be slower than np.piecewise , in particular, when the function is expensive. In some cases, it is more convenient (no lambda), and it is easier to expand the concept to many variables.

+3
source

Source: https://habr.com/ru/post/1204245/


All Articles