1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.