Skip to content

Instantly share code, notes, and snippets.

@michaelChein
Last active February 12, 2019 21:49
Show Gist options
  • Save michaelChein/3d7a78d74c73549c22f724f23dc33132 to your computer and use it in GitHub Desktop.
Save michaelChein/3d7a78d74c73549c22f724f23dc33132 to your computer and use it in GitHub Desktop.
Euclidean code for medium broadcasting
import numpy as np
from sklearn.datasets import load_iris
Load data:
data, _ = load_iris(return_X_y=True)
def euc_matrix(A):
# generate distance matrix:
B = np.rot90(A[:,:,None],1,(1,2)) #[:,:,None] is needed to add a dimension
C = np.rot90(B,1,(0,1))
return np.sqrt(np.sum((B-C)**2, axis=2))
mat = euc_matrix(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment