我试图将马氏距离计算为使用 PCA 转换后的欧几里得距离,但是,我没有得到相同的结果。以下代码:
import numpy as np
from scipy.spatial.distance import mahalanobis
from sklearn.decomposition import PCA
X = [[1,2], [2,2], [3,3]]
mean = np.mean(X, axis=0)
cov = np.cov(X, rowvar=False)
covI = np.linalg.inv(cov)
maha = mahalanobis(X[0], mean, covI)
print(maha)
pca = PCA()
X_transformed = pca.fit_transform(X)
stdev = np.std(X_transformed, axis=0)
X_transformed /= stdev
print(np.linalg.norm(X_transformed[0]))
印刷
1.1547005383792515
1.4142135623730945
据我了解,PCA 与维度无关,除以标准差对每个维度的权重均等,因此欧几里德距离应等于马氏距离。我哪里错了?