200629-Python实现Matlab中的mahal函数

Python自定义函数

def mathal(Y,X):
    mean = X.mean(axis=0)             # Calculate mean of X
    cova = np.linalg.inv(np.cov(X.T)) # Calculate covariance of X
    delta=Y-mean                      # Calculate distance between Y and mean
    d = []                            # Create a list to record the distance
    for idx in range(len(Y)):
        mahdis = np.sqrt(np.dot(np.dot(delta[idx],cova),delta[idx].T))
        d.append(mahdis)
    return d

指定相同的随机数据并调用Python


X = np.array([[0.64547261, 0.37195198],
              [0.75928248, 0.94615187],
              [0.9253419 , 0.3775274 ],
              [0.95091327, 0.00263419],
              [0.903695  , 0.86591346],
              [0.27266307, 0.82247242],
              [0.73432208, 0.83863224],
              [0.15160414, 0.47058361],
              [0.76716016, 0.38563361],
              [0.01367519, 0.34022205]])

Y = np.array([[0.04419082, 0.72423908],
              [0.47013556, 0.15783494],
              [0.57054014, 0.04820738],
              [0.67246173, 0.36585453],
              [0.05358777, 0.96579163],
              [0.44420245, 0.17962022],
              [0.08300345, 0.89841078],
              [0.1468351 , 0.32930926],
              [0.81858441, 0.64715493],
              [0.53428493, 0.91378168]])

d = mathal(Y,X)
for i in d:
    print(i)

3.098957564113144
1.752262199651384
2.599603972119247
0.35618893466106993
4.511576198655802
1.6516920549508667
3.6957851029680877
2.377123040368613
0.48941188874536307
1.5024702816713869

指定相同的随机数据并调用Matlab

X = [[0.64547261 0.37195198]
 [0.75928248 0.94615187]
 [0.9253419  0.3775274 ]
 [0.95091327 0.00263419]
 [0.903695   0.86591346]
 [0.27266307 0.82247242]
 [0.73432208 0.83863224]
 [0.15160414 0.47058361]
 [0.76716016 0.38563361]
 [0.01367519 0.34022205]]

Y = [[0.04419082 0.72423908]
 [0.47013556 0.15783494]
 [0.57054014 0.04820738]
 [0.67246173 0.36585453]
 [0.05358777 0.96579163]
 [0.44420245 0.17962022]
 [0.08300345 0.89841078]
 [0.1468351  0.32930926]
 [0.81858441 0.64715493]
 [0.53428493 0.91378168]]

mahal(Y,X))
ans =

    3.0990
    1.7523
    2.5996
    0.3562
    4.5116
    1.6517
    3.6958
    2.3771
    0.4894
    1.5025
展开阅读全文
©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值