I am interested in calculating a large NumPy array. I have a large array A
which contains a bunch of numbers. I want to calculate the sum of different combinations of these numbers. The structure of the data is as follows:
A = np.random.uniform(0,1, (3743, 1388, 3))
Combinations = np.random.randint(0,3, (306,3))
Final_Product = np.array([ np.sum( A*cb, axis=2) for cb in Combinations])
My question is if there is a more elegant and memory efficient way to calculate this? I find it frustrating to work with np.dot()
when a 3-D array is involved.
If it helps, the shape of Final_Product
ideally should be (3743, 306, 1388). Currently Final_Product
is of the shape (306, 3743, 1388), so I can just reshape to get there.
Instead of
dot
you could usetensordot
. Your current method is equivalent to:Note the
transpose
at the end to put the axes in the correct order.Like
dot
, thetensordot
function can call down to the fast BLAS/LAPACK libraries (if you have them installed) and so should be perform well for large arrays.np.dot()
won't give give you the desired output , unless you involve extra step(s) that would probably includereshaping
. Here's onevectorized
approach usingnp.einsum
to do it one shot without any extra memory overhead -For completeness, here's with
np.dot
andreshaping
as discussed earlier -Runtime tests and verify output -