Class: Array

Inherits:
Object show all
Defined in:
lib/decisiontree/id3_tree.rb

Instance Method Summary collapse

Instance Method Details

#classificationObject



17
# File 'lib/decisiontree/id3_tree.rb', line 17

def classification; collect { |v| v.last }; end

#entropyObject

calculate information entropy



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/decisiontree/id3_tree.rb', line 20

def entropy
  return 0 if empty?

  info = {}
  total = 0
  each {|i| info[i] = !info[i] ? 1 : (info[i] + 1); total += 1}

  result = 0
  info.each do |symbol, count|
    result += -count.to_f/total*Math.log(count.to_f/total)/Math.log(2.0) if (count > 0)
  end
  result
end