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?