Multiply high order matrices with numpy

I created this toy problem that reflects my much bigger problem:

import numpy as np

ind = np.ones((3,2,4)) #shape=(3L, 2L, 4L)
dist = np.array([[0.1,0.3],[1,2],[0,1]]) #shape=(3L, 2L)

ans = np.array([np.dot(dist[i],ind[i]) for i in xrange(dist.shape[0])]) #shape=(3L, 4L)
print ans
""" prints:
[[ 0.4  0.4  0.4  0.4]
 [ 3.   3.   3.   3. ]
 [ 1.   1.   1.   1. ]]
"""

I want to use numpy’s functions to calculate ans, since this operation is heavy and my matrices are huge.

I saw this post, but the shapes are different and I cannot understand which axes I should use for this problem. However, I’m certain that tensordot should be the answer. Any suggestions?


Source: python

Leave a Reply