module Enumerable

Public Instance Methods

concitional_entropy_with(label) click to toggle source
# 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
entropy() click to toggle source
# 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