mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-28 15:14:52 +00:00
Style changes
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb'
|
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb'
|
||||||
|
require 'core_extension/array.rb'
|
||||||
|
|||||||
@@ -13,32 +13,6 @@ class Object
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Array
|
|
||||||
def classification
|
|
||||||
collect(&:last)
|
|
||||||
end
|
|
||||||
|
|
||||||
# calculate information entropy
|
|
||||||
def entropy
|
|
||||||
return 0 if empty?
|
|
||||||
|
|
||||||
info = {}
|
|
||||||
total = 0
|
|
||||||
each do |i|
|
|
||||||
info[i] = !info[i] ? 1 : (info[i] + 1)
|
|
||||||
total += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
result = 0
|
|
||||||
info.each do |_symbol, count|
|
|
||||||
if count > 0
|
|
||||||
result += -count.to_f / total * Math.log(count.to_f / total) / Math.log(2.0)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module DecisionTree
|
module DecisionTree
|
||||||
Node = Struct.new(:attribute, :threshold, :gain)
|
Node = Struct.new(:attribute, :threshold, :gain)
|
||||||
|
|
||||||
@@ -105,9 +79,10 @@ module DecisionTree
|
|||||||
fitness = fitness_for(best.attribute)
|
fitness = fitness_for(best.attribute)
|
||||||
case type(best.attribute)
|
case type(best.attribute)
|
||||||
when :continuous
|
when :continuous
|
||||||
data.partition do |d|
|
partitioned_data = data.partition do |d|
|
||||||
d[attributes.index(best.attribute)] >= best.threshold
|
d[attributes.index(best.attribute)] >= best.threshold
|
||||||
end.each_with_index do |examples, i|
|
end
|
||||||
|
partitioned_data.each_with_index do |examples, i|
|
||||||
tree[best][String.new(l[i])] = id3_train(examples, attributes, (data.classification.mode rescue 0), &fitness)
|
tree[best][String.new(l[i])] = id3_train(examples, attributes, (data.classification.mode rescue 0), &fitness)
|
||||||
end
|
end
|
||||||
when :discrete
|
when :discrete
|
||||||
|
|||||||
Reference in New Issue
Block a user