Python 曼哈顿距离 切比雪夫距离 闵可夫斯基距离 标准化欧氏距离 马氏距离 编辑距离

1. 曼哈顿距离

def Manhattan(vec1, vec2):
    npvec1, npvec2 = np.array(vec1), np.array(vec2)
    return np.abs(npvec1-npvec2).sum()
# Manhattan_Distance,

2. 切比雪夫距离

def Chebyshev(vec1, vec2):
    npvec1, npvec2 = np.array(vec1), np.array(vec2)
    return max(np.abs(npvec1-npvec2))
# Chebyshev_Distance

3. 闵可夫斯基距离

#!/usr/bin/env python
 
from math import*
from decimal import Decimal
 
def nth_root(value,n_root):
    root_value=1/float(n_root)
    return round(Decimal(value)**Decimal(root_value),3)
 
def minkowski_distance(x,y,p_value):
    return nth_root(sum(pow(abs(a-b),p_value) for a,b in zip(x,y)),p_value)
 
print(minkowski_distance([0,3,4,5],[7,6,3,-1],3))

4. 标准化欧氏距离

def Standardized_Euclidean(vec1,vec2,v):
    from scipy import spatial
    npvec = np.array([np.array(vec1), np.array(vec2)])
    return spatial.distance.pdist(npvec, 'seuclidean', V=None)
# Standardized Euclidean distance
# http://blog.csdn.net/jinzhichaoshuiping/article/details/51019473


5. 马氏距离

def Mahalanobis(vec1, vec2):
    npvec1, npvec2 = np.array(vec1), np.array(vec2)
    npvec = np.array([npvec1, npvec2])
    sub = npvec.T[0]-npvec.T[1]
    inv_sub = np.linalg.inv(np.cov(npvec1, npvec2))
    return math.sqrt(np.dot(inv_sub, sub).dot(sub.T))
# MahalanobisDistance
6. 编辑距离

def Edit_distance_str(str1, str2):
    import Levenshtein
    edit_distance_distance = Levenshtein.distance(str1, str2)
    similarity = 1-(edit_distance_distance/max(len(str1), len(str2)))
    return {'Distance': edit_distance_distance, 'Similarity': similarity}
# Levenshtein distance

你可能感兴趣的:(Python 曼哈顿距离 切比雪夫距离 闵可夫斯基距离 标准化欧氏距离 马氏距离 编辑距离)