Reputation: 831
I am calculating a matrix with numpy/scipy like this:
cost = np.empty([chroma1.data.shape[1], chroma2.data.shape[1]])
for x, cx in enumerate(chroma1.transpose()):
for y, cy in enumerate(chroma2.transpose()):
cost[x, y] = sp.distance.euclidean(cx, cy)
This takes quite an amount of time. Is there any numpy/scipy function that would allow me to get rid of the two nested loops?
Upvotes: 1
Views: 1492
Reputation: 284552
It looks like you're calculating a distance matrix. scipy.spatial.distance
contains several specialized, optimized functions for doing exactly that.
In your case:
cost = scipy.spatial.distance.cdist(chroma1.T, chroma2.T)
should do exactly what you want.
Upvotes: 3