Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- entropyvctr = function(vctr) {
- total = sum(vctr)
- totalh = Reduce(function(acc, element) {
- prb = element / total
- h = if(prb == 0) 0 else prb*log2(prb)
- return (acc - h)
- }, vctr, 0)
- return (totalh)
- }
- entropy = function(assign, truth) {
- clusterlvls = levels(assign)
- clusterclss = table(assign, truth)
- n = nrow(clusterclss)
- entropywght = array(dim = c(n,2))
- pop = 0
- for (i in 1:n) {
- clustersz = sum(clusterclss[i,])
- pop = pop + clustersz
- entropywght[i,] = c(entropyvctr(clusterclss[i,]), clustersz)
- }
- perc = 0
- if (clusterlvls[1] == "0") {
- outliers = sum(clusterclss[1,])
- perc = outliers / pop
- pop = pop - outliers
- entropywght[1,] = c(0,0)
- }
- totalh = 0
- if (pop > 0) {
- for (i in 1:n) {
- totalh = totalh + entropywght[i,1] * entropywght[i,2] / pop
- }
- }
- return(c(totalh, perc))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement