Euclidean distances (python3, sklearn): efficiently compute closest pairs and their corresponding distances
I'm given a 2-D numpy array X consisting of floating values and need to compute the euclidean distances between all pairs of rows, then compute the top k row indices with the smallest distances and return them (where k > 0). I'm testing with a small array and this is what I have so far... import numpy as np from sklearn.metrics.pairwise import euclidean_distances X_testing = np.asarray([[1,2,3.5],[4,1,2],[0,0,2],[3.4,1,5.6]]) test = euclidean_distances(X_testing, X_testing) print(test) The resulting printout is: [[ 0. 3.5 2.6925824 3.34215499] [ 3.5 0. 4.12310563 3.64965752] [ 2.6925824 4