【问题标题】:Centroid distance calculation in PCA space and in 'feature-space' divergePCA 空间和“特征空间”中的质心距离计算发散
【发布时间】:2017-04-21 11:44:52
【问题描述】:

我正在测量 PCA 空间的质心和跨越约 20 个治疗和 3 个组的“特征空间”。如果我正确理解我的数学老师,他们之间的距离应该是相同的。然而,在我计算它们的方式上,它们不是,我想知道我做数学的方式是否是错误的。

我使用臭名昭著的葡萄酒数据集作为我的方法/MWE 的说明:

library(ggbiplot)
data(wine)
treatments <- 1:2 #treatments to be considerd for this calculation
wine.pca <- prcomp(wine[treatments], scale. = TRUE)
#calculate the centroids for the feature/treatment space and the pca space
df.wine.x <- as.data.frame(wine.pca$x)
df.wine.x$groups <- wine.class
wine$groups <- wine.class
feature.centroids <- aggregate(wine[treatments], list(Type = wine$groups), mean)
pca.centroids <- aggregate(df.wine.x[treatments], list(Type = df.wine.x$groups), mean)
pca.centroids
feature.centroids
#calculate distance between the centroids of barolo and grignolino
dist(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 代表特征空间中的距离和 1.80717 代表 pca 空间中的距离,表明美中不足...

【问题讨论】:

    标签: r machine-learning distance data-mining pca


    【解决方案1】:

    这是因为缩放和居中,如果您不进行缩放和居中,原始和 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.468087
    dist(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.80717
    dist(rbind(pca.centroids[pca.centroids$Type == "barolo",][-1],pca.centroids[pca.centroids$Type == "grignolino",][-1]), method = "euclidean")
    #        1
    # 2 1.80717
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      相关资源
      最近更新 更多