Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. entropyvctr = function(vctr) {
  2. total = sum(vctr)
  3. totalh = Reduce(function(acc, element) {
  4. prb = element / total
  5. h = if(prb == 0) 0 else prb*log2(prb)
  6. return (acc - h)
  7. }, vctr, 0)
  8. return (totalh)
  9. }
  10.  
  11. entropy = function(assign, truth) {
  12. clusterlvls = levels(assign)
  13. clusterclss = table(assign, truth)
  14.  
  15. n = nrow(clusterclss)
  16. entropywght = array(dim = c(n,2))
  17. pop = 0
  18.  
  19. for (i in 1:n) {
  20. clustersz = sum(clusterclss[i,])
  21. pop = pop + clustersz
  22. entropywght[i,] = c(entropyvctr(clusterclss[i,]), clustersz)
  23. }
  24.  
  25. perc = 0
  26. if (clusterlvls[1] == "0") {
  27. outliers = sum(clusterclss[1,])
  28. perc = outliers / pop
  29. pop = pop - outliers
  30. entropywght[1,] = c(0,0)
  31. }
  32.  
  33. totalh = 0
  34. if (pop > 0) {
  35. for (i in 1:n) {
  36. totalh = totalh + entropywght[i,1] * entropywght[i,2] / pop
  37. }
  38. }
  39.  
  40. return(c(totalh, perc))
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement