我正在分析R中的数据,其中有预测变量,但没有响应变量。使用无监督学习(k均值),我识别了数据中的模式。但是,我需要根据集群的整体表现(例如:学生的考试成绩数据和课外成绩)对集群进行排序。在R中聚类之后,我使用了什么技术?
发布于 2022-06-03 02:40:32
kmeans输出的cluster属性为您提供了每个数据点所在的集群的索引。从kmeans文档获取的示例数据:
nclusters = 5
# a 2-dimensional example
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
cl <- kmeans(x, nclusters, nstart = 25)现在,您的评估函数(例如,列值的平均值)可以单独应用于每个集群:
for (i in 1:nclusters){
cat(i, apply(x[which(cl$cluster==i),],MARGIN=2,FUN=mean), '\n')
}或者更好的是,使用某种聚合函数,例如tapply或aggregate,例如:
aggregate(x, by=list(cluster=cl$cluster), FUN=mean)这给
cluster x y
1 1 1.2468266 1.1499059
2 2 -0.2787117 0.0958023
3 3 0.5360855 1.0217910
4 4 1.0997776 0.7175210
5 5 0.2472313 -0.1193551此时,您应该能够根据需要对聚合函数的值进行排序。
https://stackoverflow.com/questions/72484078
复制相似问题