豪斯多夫(Hausdorff)距离的python实现(附具体代码)9.21

引:在将欧式距离替换成豪斯多夫距离时发现讲述理论的较多,而具体实操的较少,有C++版本和matlab版本的但都过于繁复,因此自己利用python编写了以下实现过程,如有需要可以自取。
留个坑有空的话来自己梳理以下原理

#豪斯多夫python实现过程
import numpy as np
import operator

A=[[1,3,3],
   [4,5,6]]
B=[[1,2,3],
   [4,8,7]]

asize = [len(A),len(A[0])]
bsize = [len(B),len(B[0])]

if asize[1] != bsize[1]:
    print('The dimensions of points in the two sets are not equal')

    
fhd = 0
for i in range(asize[0]):
    mindist = float("inf")
    for j in range(bsize[0]):
        tempdist = np.linalg.norm(list(map(operator.sub, A[i], B[j])))
        if tempdist < mindist:
            mindist = tempdist
    fhd = fhd + mindist
fhd = fhd/asize[0]
fhd

rhd = 0
for j in range(bsize[0]):
    mindist = float("inf")
    for i in range(asize[0]):
        tempdist = np.linalg.norm(list(map(operator.sub, A[i], B[j])))
        if tempdist < mindist:
            mindist = tempdist
    rhd = rhd + mindist
rhd = rhd/bsize[0]

mhd = max(fhd,rhd)

fhd,rhd,mhd

你可能感兴趣的:(代码精进,python)