我正在使用e1071
包运行模糊 C 均值聚类。我想根据以下公式中给出的模糊性能指数(FPI)(模糊程度)和归一化分类熵(NCE)(特定类的无组织程度)来确定最佳聚类数
其中 c 是聚类数,n 是观测数,μ ik是模糊隶属度,log a是自然对数。
我正在使用以下代码
library(e1071)
x <- rbind(matrix(rnorm(100,sd=0.3),ncol=2),
matrix(rnorm(100,mean=1,sd=0.3),ncol=2))
cl <- cmeans(x,2,20,verbose=TRUE,method="cmeans")
cl$membership
我已经能够提取 μ ik即模糊隶属度。现在,cmeans
必须针对不同数量的集群,例如 2 到 6,并且必须计算 FPI 和 NCE 以获得如下图
如何在 R 中实现?
编辑
iris
我已经使用以下代码尝试了@nya 为数据集提供的代码
df <- scale(iris[-5])
FPI <- function(cmem){
c <- ncol(cmem)
n <- nrow(cmem)
1 - (c / (c - 1)) * (1 - sum(cmem^2) / n)
}
NCE <- function(cmem){
c <- ncol(cmem)
n <- nrow(cmem)
(n / (n - c)) * (- sum(cmem * log(cmem)) / n)
}
# prepare variables
cl <- list()
fpi <- nce <- NULL
# cycle through the desired number of clusters
for(i in 2:6){
cl[[i]] <- cmeans(df, i, 20, method = "cmeans")
fpi <- c(fpi, FPI(cl[[i]]$membership))
nce <- c(nce, NCE(cl[[i]]$membership))
}
# add space for the second axis label
par(mar = c(5,4,1,4) + .1)
# plot FPI
plot(2:6, fpi, lty = 2, pch = 18, type = "b", xlab = "Number of clusters", ylab = "FPI")
# plot NCE, manually adding the second axis
par(new = TRUE)
plot(2:6, nce, lty = 1, pch = 15, type = "b", xlab = "", ylab = "", axes = FALSE)
axis(4, at = pretty(range(nce)))
mtext("NCE", side = 4, line = 3)
# add legend
legend("top", legend = c("FPI", "NCE"), pch = c(18,15), lty = c(2,1), horiz = TRUE)
考虑模糊性能指数(FPI)和归一化分类熵(NCE)的最小值来确定最佳聚类数。NCE 一直在增加,而 FPI 则呈下降趋势。理想情况下应该是