Numpy: Efficient matrix calculation A*xj where xj is row j in X

Question

So the background to this "problem" is that I'm trying to optimize a large python project. I started timing the program and noticed that almost 50% of the run time is spent on a calculation similar to this one:

``````import numpy as np

# Example
A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
X = np.random.multivariate_normal([0,0,0,0],np.eye(4),15000)

# Create a lambda function to use row based
F = lambda x: np.dot(A,x)

# Now calculating the value
answer = np.apply_along_axis(F, 1, X)

``````

I've tried to find a way to make this faster, but keep running into a wall. Is this really the optimal of doing this?

Show source
2016-12-21 13:12 1 Answers

Answers to Numpy: Efficient matrix calculation A*xj where xj is row j in X ( 1 )

1. We could use `np.dot` on `X` and `A` to lose their second axes each. To input into `np.dot`, we will use `X` as the first input and transpose `A` to bring its second axis to the front to be used as the second input.

Thus, we would have the output like so -

``````X.dot(A.T)
``````

Runtime test for sample inputs listed in question -

``````In [192]: %timeit np.apply_along_axis(F, 1, X)
1 loops, best of 3: 185 ms per loop

In [193]: %timeit X.dot(A.T)
1000 loops, best of 3: 228 µs per loop

In [194]: np.allclose(np.apply_along_axis(F, 1, X), X.dot(A.T))
Out[194]: True # verified results against original code
``````