PCA空间和’特征空间’中的质心距离计算差异

我正在测量一个PCA空间和一个’特征空间’的质心,这两个空间涵盖了大约20种处理和3个组。如果我正确理解了我数学老师的讲解,这两个空间之间的距离应该是相同的。然而,我计算的结果显示它们并不相同,我想知道我的计算方法是否有误。

我使用著名的葡萄酒数据集作为我的方法/最小工作示例的说明:

library(ggbiplot)data(wine)treatments <- 1:2 #treatments to be considerd for this calculationwine.pca <- prcomp(wine[treatments], scale. = TRUE)#calculate the centroids for the feature/treatment space and the pca spacedf.wine.x <- as.data.frame(wine.pca$x)df.wine.x$groups <- wine.classwine$groups <- wine.classfeature.centroids <- aggregate(wine[treatments], list(Type = wine$groups), mean)pca.centroids <- aggregate(df.wine.x[treatments], list(Type = df.wine.x$groups), mean)pca.centroidsfeature.centroids#calculate distance between the centroids of barolo and grignolinodist(rbind(feature.centroids[feature.centroids$Type == "barolo",][-1],feature.centroids[feature.centroids$Type == "grignolino",][-1]), method = "euclidean")dist(rbind(pca.centroids[pca.centroids$Type == "barolo",][-1],pca.centroids[pca.centroids$Type == "grignolino",][-1]), method = "euclidean")

最后两行返回的特征空间距离为1.468087,而PCA空间内的距离为1.80717,这表明其中可能存在问题…


回答:

这是因为缩放和中心化的问题,如果你不进行缩放和中心化,原始特征空间和PCA特征空间的距离将完全相同。

wine.pca <- prcomp(wine[treatments], scale=FALSE, center=FALSE)dist(rbind(feature.centroids[feature.centroids$Type == "barolo",][-1],feature.centroids[feature.centroids$Type == "grignolino",][-1]), method = "euclidean")#         1# 2 1.468087dist(rbind(pca.centroids[pca.centroids$Type == "barolo",][-1],pca.centroids[pca.centroids$Type == "grignolino",][-1]), method = "euclidean")#         1# 2 1.468087

另一种获得相同结果的方法是先对原始数据进行缩放/中心化处理,然后再应用带有缩放/中心化的PCA,如下所示:

wine[treatments] <- scale(wine[treatments], center = TRUE)wine.pca <- prcomp(wine[treatments], scale = TRUE)dist(rbind(feature.centroids[feature.centroids$Type == "barolo",][-1],feature.centroids[feature.centroids$Type == "grignolino",][-1]), method = "euclidean")#        1# 2 1.80717dist(rbind(pca.centroids[pca.centroids$Type == "barolo",][-1],pca.centroids[pca.centroids$Type == "grignolino",][-1]), method = "euclidean")#        1# 2 1.80717

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注