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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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