You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何处理PCA双图中的大量异常值,兼顾所有样本可视化?

解决PCA可视化中极端异常值导致的失真问题

你可以通过以下几种实用方法调整,让所有国家的PCA结果都能清晰展示:

1. 移除异常值后重新绘图

直接过滤掉极端异常值(比如美国),基于剩余数据生成坐标并绘图:

# 假设test_data_PCA包含country列,过滤掉美国
filtered_data <- test_data_PCA[test_data_PCA$country != "USA", ]
countries.coord_filtered <- predict(res.pca, newdata = filtered_data)
# 隐藏原始样本点,只展示过滤后的国家
p <- fviz_pca_ind(res.pca, repel = TRUE, invisible = "ind")
fviz_add(p, countries.coord_filtered, color = "blue")

2. 手动限制坐标轴范围

裁剪坐标轴到合理范围,聚焦大部分样本的展示区域:

p <- fviz_pca_ind(res.pca, repel = TRUE)
# 根据你的数据实际分布调整x、y轴范围
fviz_add(p, countries.coord, color = "blue") +
  xlim(-4, 4) + ylim(-4, 4)

3. 对变量做对数转换

如果异常值源于部分数值变量量级过大,先对变量做对数转换再重新计算PCA:

# 对数值型变量做对数转换(+1避免log(0)的情况)
test_data_log <- test_data_PCA
num_cols <- sapply(test_data_log, is.numeric)
test_data_log[, num_cols] <- log(test_data_log[, num_cols] + 1)
# 重新计算PCA并绘图
res.pca_log <- prcomp(test_data_log[, num_cols], scale. = TRUE)
countries.coord_log <- predict(res.pca_log, newdata = test_data_log)
p <- fviz_pca_ind(res.pca_log, repel = TRUE)
fviz_add(p, countries.coord_log, color = "blue")

4. 缩放异常值坐标并标注说明

手动缩放异常值的坐标到可见范围,同时标注提示:

# 找到美国对应的坐标行
usa_row <- rownames(countries.coord) == "USA"
# 按比例缩小异常值坐标(系数根据实际情况调整)
countries.coord[usa_row, ] <- countries.coord[usa_row, ] * 0.2
# 绘图并添加标注
p <- fviz_pca_ind(res.pca, repel = TRUE)
fviz_add(p, countries.coord, color = "blue") +
  geom_text(data = as.data.frame(countries.coord)[usa_row, ],
            aes(x = PC1, y = PC2, label = "USA (坐标已缩放)"),
            color = "red", vjust = 1.2)

内容的提问来源于stack exchange,提问作者Kash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 07:00:59