我正在R中运行潜在类分析并使用熵函数。我想了解为什么在输出中,它为低nclass生成结果,然后为高nclass生成NaN。
我是该软件的初学者!
作为参考,是输出和代码:
> entropy<-function (p) sum(-p*log(p))
> error_prior <- entropy(France_2class$P) # Class proportions
> error_post <- mean(apply(France_2class$posterior, 1, entropy))
> R2_entropy <- (error_prior - error_post) / error_prior
> R2_entropy
[1] 0.8121263
>
> entropy<-function (p) sum(-p*log(p))
> error_prior <- entropy(France_3class$P) # Class proportions
> error_post <- mean(apply(France_3class$posterior, 1, entropy))
> R2_entropy <- (error_prior - error_post) / error_prior
> R2_entropy
[1] 0.8139903
>
> entropy<-function (p) sum(-p*log(p))
> error_prior <- entropy(France_4class$P) # Class proportions
> error_post <- mean(apply(France_4class$posterior, 1, entropy))
> R2_entropy <- (error_prior - error_post) / error_prior
> R2_entropy
[1] NaN
>
> entropy<-function (p) sum(-p*log(p))
> error_prior <- entropy(France_5class$P) # Class proportions
> error_post <- mean(apply(France_5class$posterior, 1, entropy))
> R2_entropy <- (error_prior - error_post) / error_prior
> R2_entropy
[1] NaN
>
> entropy<-function (p) sum(-p*log(p))
> error_prior <- entropy(France_6class$P) # Class proportions
> error_post <- mean(apply(France_6class$posterior, 1, entropy))
> R2_entropy <- (error_prior - error_post) / error_prior
> R2_entropy
[1] NaN
有人可以帮忙吗?谢谢
答案 0 :(得分:1)
我想问题出在entropy
的定义上。更准确地说,如果0
中包含p
,那么您将获得NaN
,例如,
> entropy(p1)
[1] 1.279854
> entropy(p2)
[1] NaN
> entropy(p3)
[1] 0.5004024
要解决此问题,您可以像下面这样向功能na.omit
添加entropy
entropy<-function(p) sum(na.omit(-p*log(p)))
然后您可以看到
> entropy(p1)
[1] 1.279854
> entropy(p2)
[1] 0.5004024
> entropy(p3)
[1] 0.5004024
数据
p1 <- c(0.1,0.2,0.3,0.4)
p2 <- c(0,0.2,0.8)
p3 <- c(0.2,0.8)