python求明氏距离

何为明式距离

$ P = (x{1},x{2},\cdot \cdot \cdot,x{n}) and Q=(y{1},y{2},\cdot \cdot \cdot,y{n}) \in R^n $

$ D(X,Y) = \bigg( \sum{i=1}^n |x{i} - y_{i}|^p\bigg)^{1/p} $

假设P = (0,4),Q=(3,0),求P,Q两点的明氏距离。

import numpy as np
from scipy.spatial import minkowski_distance
a = np.array([[0,4]])
a = np.array([[3,0]])
dist = minkowski_distance(a,b,1) #array([7])
dist = minkowski_distance(a,b,2) #array([5.])
dist = minkowski_distance(a,b,3) #array([4.49794145])

$ \lim{p \to \infty} \bigg( \sum{i=1}^n |x{i}-y{i}|^p\bigg)^{1/p} = \max{i=1}^n |x{i}-y_{i}| $ ,当p—>oo时,P,Q两点的距离等于4

from sympy import symbols, limit
p = symbols('p')
f = (3**p + 4**p)**(1/p) 
limit(f,p,oo)           # 4