R 中的无监督学习
Hank Roark
Senior Data Scientist at Boeing


# 初始化组内平方和误差:wss
wss <- 0
# 遍历1到15个聚类数
for (i in 1:15) {
# 拟合模型:km.out
km.out <- kmeans(pokemon, centers = i, nstart = 20, iter.max = 50)
# 保存组内平方和
wss[i] <- km.out$tot.withinss
}
# 绘制碎石图
plot(1:15, wss, type = "b",
xlab = "Number of Clusters",
ylab = "Within groups sum of squares")


pr.iris <- prcomp(x = iris[-5],
scale = FALSE,
center = TRUE)
summary(pr.iris)
主成分的重要性:
PC1 PC2 PC3 PC4
标准差 2.0563 0.49262 0.2797 0.15439
方差占比 0.9246 0.05307 0.0171 0.00521
累计占比 0.9246 0.97769 0.9948 1.00000


# 对主成分1和3重复作图
plot(wisc.pr$x[, c(1, 3)], col = (diagnosis + 1),
xlab = "PC1", ylab = "PC3")

R 中的无监督学习