Module: Enumerable

Defined in:
lib/decision-tree.rb

Instance Method Summary collapse

Instance Method Details

#concitional_entropy_with(label) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/decision-tree.rb', line 18

def concitional_entropy_with(label)
    dataset = Hash.new{|h,k| h[k] = Array.new }
    self.each_with_index{|v,i| dataset[v] << label[i] }

    new_entropy = 0.0
    dataset.each{|k,v| new_entropy += (v.size.to_f / self.size)*v.entropy }
    return new_entropy
end

#entropyObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/decision-tree.rb', line 5

def entropy
    dataset = Hash.new(0)
    self.each{|x| dataset[x] += 1 }

    entropy = 0.0
    dataset.each do |k,v|
        p = v.to_f / self.size
        entropy += (-p)*Math.log2(p)
    end

    return entropy
end