来看个题
【解】
代码验证
import numpy as np
from scipy.spatial.distance import mahalanobis
# 定义四个点坐标的数组
points = np.array([[0.5, 1.5], [4, 4], [8, 6], [2, 2]])
# 计算协方差矩阵
covariance_matrix = np.cov(points.T)
# 计算马氏距离
mean = np.mean(points, axis=0)
print("协方差矩阵:")
print(covariance_matrix)
print(mahalanobis(points[0], points[-1], np.linalg.inv(covariance_matrix)))
print(mahalanobis(points[1], points[-1], np.linalg.inv(covariance_matrix)))
print(mahalanobis(points[2], points[-1], np.linalg.inv(covariance_matrix)))