The following Euclidean distance algorithm creates a MxM matrix of distances between the rows of an MxN input matrix (representative of points in some N dimensional space). The speed of this algorithm scales in O(m^2). Can this be improved upon if only interested in the rows (i.e. points) that are closest to each other? (My downstream task constists of performing K-NN, amongst other things)
import numpy as np
vectors = np.random.randn(100, 20)
m = vectors.shape[0]
distances = np.zeros([m, m])
for i in range(m):
vec = vectors[i]
distances[i] = [np.linalg.norm(vec - vectors[j]) for j in range(m)]