import pdb import numpy as np n = 20 m = 200 d = 128 # make reproducible np.random.seed(0) X = np.random.random((n, d)) Y = np.random.random((m, d)) # squared pariwise distances pairwise_distances = np.empty((n, m)) nn_index = np.empty(n, dtype = int) for i in range(n): nearest_dist = np.inf for j in range(m): dist = pairwise_distances[i, j] = ((X[i] - Y[j]) ** 2).sum() if dist < nearest_dist: nearest_dist = dist nearest_index = j nn_index[i] = nearest_index print "indices of nearest-neighbors:", nn_index