代码是:
import numpy as np
def Mahalanobis(x, covariance_matrix, mean):
x = np.array(x)
mean = np.array(mean)
covariance_matrix = np.array(covariance_matrix)
return (x-mean)*np.linalg.inv(covariance_matrix)*(x.transpose()-mean.transpose())
#variables x and mean are 1xd arrays; covariance_matrix is a dxd matrix
#the 1xd array passed to x should be multiplied by the (inverted) dxd array
#that was passed into the second argument
#the resulting 1xd matrix is to be multiplied by a dx1 matrix, the transpose of
#[x-mean], which should result in a 1x1 array (a number)
但是由于某种原因,当我输入参数时,我得到了一个输出矩阵
Mahalanobis([2,5], [[.5,0],[0,2]], [3,6])
输出:
out[]: array([[ 2. , 0. ],
[ 0. , 0.5]])
看来我的函数只是给了我在第二个参数中输入的 2x2 矩阵的逆矩阵。