如何计算每个班级的熵?

时间:2015-10-08 10:59:17

标签: entropy

我正在学校做作业,现在已经被困了3天了,所以希望能帮助解决这个问题。

您可以看到代码被注释,但这是我的问题:我尝试计算每个类的熵,但我不知道如何。这是尝试一次计算#probability和熵,但我不知道这是否正确。有什么建议吗?

buys <- c("no", "no", "yes", "yes", "yes", "no", "yes", "no", "yes", "yes", "yes", "yes", "yes", "no")
credit <- c("fair", "excellent", "fair", "fair", "fair", "excellent", "excellent", "fair", "fair", "fair", "excellent", "excellent", "fair", "excellent")
student <- c("no", "no", "no","no", "yes", "yes", "yes", "no", "yes", "yes", "yes", "no", "yes", "no")
income <- c("high", "high", "high", "medium", "low", "low", "low", "medium", "low", "medium", "medium", "medium", "high", "medium")
age <- c(25, 27, 35, 41, 48, 42, 36, 29, 26, 45, 23, 33, 37, 44) # we change the age from categorical to numeric

data <- data.frame(age, income, student, credit, buys) # create a data frame


info <- function(CLASS.FREQ){
  freq.class <- CLASS.FREQ
  info <- 0
  for(i in 1:length(freq.class)){
    if(freq.class[[i]] != 0){ # if the number of examples in class i is not 0

      entropy<- -sum(freq.class[i]/length(freq.class) * log2(freq.class[i]/freq.class))
# this is my problem. I try to calculate the entropy for each class, but i do not know how. This is an attempt to calculate the #probability and the entropy at once, but i have ni idea if that is correct. Any advice?

          }else{
      entropy <- 0 # if we face log(0), the entropy is given 0
    }
    info <- info + entropy # sum up entropy from all classes
  }
  return(info)
}



buys.freq <- table(buys)
buys.freq
info.buys <- info(buys.freq) #while calculating the info for buys, the result should be 0.940286.
info.buys

0 个答案:

没有答案