Skip to content

Instantly share code, notes, and snippets.

@acharles7
Created July 20, 2019 04:18
Show Gist options
  • Save acharles7/11b6b469d628d8ba87f97cf63384cf47 to your computer and use it in GitHub Desktop.
Save acharles7/11b6b469d628d8ba87f97cf63384cf47 to your computer and use it in GitHub Desktop.
Compute squared L2 distance matrix
# dataset [n x d]
X = np.array(data_set)
Y = np.array([[93, 7], [50, 34], [51, 79]])
def l2-Distance(X, MU):
x = np.sum(X**2, axis=1, keepdims=True)
y = np.sum(MU**2, axis=1)
xy = np.dot(X, MU.T)
dist = np.sqrt(x - 2*xy + y)
return np.argmin(dist, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment