Skip to content
Advertisement

how to calculate the distances between all datapoints among each other

I want to check which data points within X are close to each other and which are far. by calculating the distances between each other without getting to zero, is it possible?

X = np.random.rand(20, 10)
dist = (X - X) ** 2
print(X)

Advertisement

Answer

Using just numpy you can either do,

np.linalg.norm((X - X[:,None]),axis=-1)

or,

np.sqrt(np.square(X - X[:,None]).sum(-1))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement