Megastore
Megastore

Reputation: 73

Euclidian distance between two python matrixes without double for-loop?

I am working with two numpy matrixes, U (dimensions Nu x 3) and M (dimensions 3 x Nm)

A contains Nu users and 3 features

M contains Nm movies (and the same 3 features)

For each user of U, I would like to calculate its euclidian distance to every movie in M (so I need to compute Nu*Nm euclidian distances).

Is this possible without an explicit double for-loop? I am working with large dimensions matrixes and the double for-loop will probably take too much time.

Thanks in advance.

Upvotes: 0

Views: 333

Answers (2)

xcmkz
xcmkz

Reputation: 697

Check out scipy.spatial.distance.cdist. Something like this will do:

from scipy.spatial.distance import cdist
dist = cdist(U, M.T)

Upvotes: 1

Allan Juan
Allan Juan

Reputation: 2558

I'm afraid not. You need to compute the euclidian distance for every pair of (user, movie), so you'll have a time complexity of numOfUsers * numOfMovies, which would be a double for loop. You can't do less operations than that, unless you're willing to skip some pairs. The best you can do is optimize the euclidian distance calculation, but the number of operations you're going to do will be quadratic one way or the other.

Upvotes: 0

Related Questions