简体   繁体   中英

Compute distances between 2 matrix of vector

I have a problem with Numpy broadcasting between two matrix. I need to compute the euclidean distance between 2 matrix for a knn classifier. I have already done it with two loop and one loop but it too slow. I'm looking for doing it with Numpy broadcasting without any explicit loop but I'm stuck.

The two loop version:

num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in range(num_test):
    for j in range(num_train):
        dists[i, j] = np.sqrt(np.sum(np.power(self.X_train[j, :] - X[i, :], 2)))
return dists

The one loop version:

num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in range(num_test):
    dists[i, :] = np.sqrt(np.sum(np.power(self.X_train - X[i, :], 2), axis=1))
return dists

The shape of X_train is (5000, 784) and X is (500,784). The output need to have the shape (500, 5000).

Have you any idea to help me?

You can use scipy's euclidean distance:

from scipy.spatial import distance
x1 = np.asarray([[1,2,3],[4,5,6]])
x2 = np.asarray([[1,2,40],[14,5,6]])

for i in range(0,x1.shape[0]):
    print distance.euclidean(x1[i], x2[i])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM