MKF
MKF

Reputation: 115

Most efficient way to apply linear transformations to each column of a numpy array

I have a random numpy array

import numpy as np

a = np.random.randn(10000*5).reshape((10000,5))

and I want to transform as efficiently as possible each column by the function

def lintransform(interval,x): 
    return (interval[1]-interval[0])*x + interval[0]

Where interval is one of five sorted array of length 2 to transform the columns of a.

(for example listofintervals = [[0,3],[1,9],[0.5,3],[4,10],[1,2.7]] )

What is the most efficient way to apply each this function for each column respectively and generate a new array, changing the interval used according to its position in the listofintervals?

Upvotes: 0

Views: 496

Answers (2)

FBruzzesi
FBruzzesi

Reputation: 6485

Using numpy vectorization you can do:

import numpy as np
a = np.random.randn(10000, 5)

intervals = np.array([[0,3],
                      [1,9],
                      [0.5,3],
                      [4,10],
                      [1,2.7]])

r = (intervals[:,1] - intervals[:,0]) * a + intervals[:,0]

Which takes:

%timeit (intervals[:,1] - intervals[:,0]) * a + intervals[:,0]
131 µs ± 1.35 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Upvotes: 3

Derek O
Derek O

Reputation: 19555

Looping through the columns, and applying your function to each row should work:

for col in range(a.shape[1]):
    a[col] = lintransform(listofintervals[col], a[col])

Output:

a
array([[-5.80231737, -3.1056331 , -1.3878622 ,  3.2891958 , -1.35495844],
       [-7.93085499, 18.46079707, 13.81923528, -3.18486045, -0.31541526],
       [ 1.53477244,  2.61705202, -2.14505552,  0.14751953,  4.70029497],
       ...,
       [ 1.13798389, -0.6765344 , -0.1364982 , -1.0443724 ,  0.06717867],
       [-1.78251012,  0.11171333,  1.28247762,  0.52285423,  0.16057854],
       [-0.59513499, -0.76866946, -0.37233491, -1.08463643, -0.45660967]])

Upvotes: 1

Related Questions