18 lines
327 B
Python
18 lines
327 B
Python
# coding:utf-8
|
|
import math
|
|
|
|
import numpy as np
|
|
|
|
|
|
def euclidean_distance(a, b):
|
|
if isinstance(a, list) and isinstance(b, list):
|
|
a = np.array(a)
|
|
b = np.array(b)
|
|
|
|
return math.sqrt(sum((a - b) ** 2))
|
|
|
|
|
|
def l2_distance(X):
|
|
sum_X = np.sum(X * X, axis=1)
|
|
return (-2 * np.dot(X, X.T) + sum_X).T + sum_X
|