python—马氏距离

https://blog.csdn.net/qq_45717425/article/details/120651436
python代码实现上述理论

#encoding='utf-8’
import numpy as np

def mas_distance(target_matrix,comp1=0,comp2=-1):
    mas_dis = np.mat(0)
    C1 = np.mat(target_matrix[comp1])
    C2 = np.mat(target_matrix[comp2])
    P1 =  C1- C2
    S = np.cov(target_matrix,rowvar=0)
    P2 =np.linalg.inv(S)
    P3 = P1.T
    mas_dis = P1*P2*P3
    return np.sqrt(mas_dis[0,0])

# matrix = np.empty(shape=[0, 3])
# matrix = np.append(matrix,np.array([10,15,29]).reshape(1,3),axis=0)
# matrix = np.append(matrix,np.array([15,46,13]).reshape(1,3),axis=0)
# matrix = np.append(matrix,np.array([23,21,30]).reshape(1,3),axis=0)
# matrix = np.append(matrix,np.array([11,9,35]).reshape(1,3),axis=0)
# print(matrix)
#
# print(mas_distance(matrix,1))

你可能感兴趣的:(python)