gpt4 book ai didi

r - 如何计算R中的模糊性能指标和归一化分类熵

转载 作者:行者123 更新时间:2023-12-05 01:56:40 26 4
gpt4 key购买 nike

我正在使用 e1071 包运行模糊 C 均值聚类。我想根据以下公式给出的模糊性能指数(FPI)(模糊程度)和归一化分类熵(NCE)(特定类别的困惑程度)来确定最佳聚类数

enter image description here

其中 c 是聚类数,n 是观察数,μik 是模糊隶属度,loga 是自然对数。

我正在使用下面的代码

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 才能得到如下图

enter image description here

如何在R中实现?

编辑

我已经使用以下代码尝试了@nya 为 iris 数据集提供的代码

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)

enter image description here

考虑模糊性能指数 (FPI) 和归一化分类熵 (NCE) 的最小值来确定最佳聚类数。 NCE 一直在增加,而 FPI 则显示值(value)下降。理想情况下应该是

enter image description here

最佳答案

利用可用的方程式,我们可以编写自己的函数。在这里,这两个函数使用了您建议的论文中的方程式和作者引用的引用文献之一。

FPI <- function(cmem, method = c("FuzME", "McBrathney", "Rahul")){
method = match.arg(method)
C <- ncol(cmem)
N <- nrow(cmem)

# Rahul et al. 2019. https://doi.org/10.1080/03650340.2019.1578345
if(method == "Rahul"){
res <- 1 - (C / (C - 1)) * (1 - sum(cmem^2) / N)
}
# McBrathney & Moore 1985 https://doi.org/10.1016/0168-1923(85)90082-6
if(method == "McBrathney"){
F <- sum(cmem^2) / N
res <- 1 - (C * F - 1) / (F - 1)
}
# FuzME https://precision-agriculture.sydney.edu.au/resources/software/
# MATLAB code file fvalidity.m, downloaded on 11 Nov, 2021
if(method == "FuzME"){
F <- sum(cmem^2) / N
res <- 1 - (C * F - 1) / (C - 1)
}
return(res)
}

NCE <- function(cmem, method = c("FuzME", "McBrathney", "Rahul")){
method = match.arg(method)
C <- ncol(cmem)
N <- nrow(cmem)

if(method == "Rahul"){
res <- (N / (N - C)) * (- sum(cmem * log(cmem)) / N)
}
if(method %in% c("FuzME", "McBrathney")){
H <- -1 / N * sum(cmem * log(cmem))
res <- H / log(C)
}
return(res)
}

然后使用这些从 iris 数据集的 cmeans 函数的隶属度计算指数。

# prepare variables
cl <- list()
fpi <- nce <- NULL

# cycle through the desired number of clusters
for(i in 2:6){
cl[[i]] <- e1071::cmeans(iris[, -5], i, 20, method = "cmeans")
fpi <- c(fpi, FPI(cl[[i]]$membership, method = "M"))
nce <- c(nce, NCE(cl[[i]]$membership, method = "M"))
}

最后,绘制 two different axes在一个情节中。

# 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)

enter image description here

EDIT1:根据来自两个不同出版物的可选方程更新函数,并在 iris 数据集上计算示例。

EDIT2: 添加了在可用的 FuzME MATLAB 代码中指定的 FPI 和 NCE 计算的代码 here .

关于r - 如何计算R中的模糊性能指标和归一化分类熵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69738591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com