Skip to content

Instantly share code, notes, and snippets.

@SemantiveCode
Created December 18, 2018 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SemantiveCode/986c31a0e2e517dff209f1872731140d to your computer and use it in GitHub Desktop.
Save SemantiveCode/986c31a0e2e517dff209f1872731140d to your computer and use it in GitHub Desktop.
import numpy as np
def dummy_mat(mat_v, mat_u):
return [sum((v_i - u_i)**2 for v_i, u_i in zip(v, u))**0.5 for v, u in zip(mat_v, mat_u)]
def bare_numpy_mat(mat_v, mat_u):
return np.sqrt(np.sum((mat_v - mat_u) ** 2, axis=1))
def l2_norm_mat(mat_v, mat_u):
return np.linalg.norm(mat_v - mat_u, axis=1)
def scipy_distance_mat(mat_v, mat_u):
# Unfortunately, the scipy_distance only works on 1D-arrays, so we are not able to vectorize it again.
return list(map(distance.euclidean, mat_v, mat_u))
def einsum_mat(mat_v, mat_u):
mat_z = mat_v - mat_u
return np.sqrt(np.einsum('ij,ij->i', mat_z, mat_z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment