mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-27 22:54:52 +00:00
Simplify with #sum
This commit is contained in:
BIN
lib/.DS_Store
vendored
Normal file
BIN
lib/.DS_Store
vendored
Normal file
Binary file not shown.
@@ -2,9 +2,10 @@ class Array
|
|||||||
def entropy
|
def entropy
|
||||||
each_with_object(Hash.new(0)) do |i, result|
|
each_with_object(Hash.new(0)) do |i, result|
|
||||||
result[i] += 1
|
result[i] += 1
|
||||||
end.values.inject(0) do |sum, count|
|
end.values.sum do |count|
|
||||||
percentage = count.to_f / length
|
percentage = count.to_f / length
|
||||||
sum + -percentage * Math.log2(percentage)
|
|
||||||
|
-percentage * Math.log2(percentage)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -120,12 +120,12 @@ module DecisionTree
|
|||||||
index = attributes.index(attribute)
|
index = attributes.index(attribute)
|
||||||
|
|
||||||
values = data.map { |row| row[index] }.uniq
|
values = data.map { |row| row[index] }.uniq
|
||||||
remainder = values.sort.inject(0) do |sum, val|
|
remainder = values.sort.sum do |val|
|
||||||
classification = data.each_with_object([]) do |row, result|
|
classification = data.each_with_object([]) do |row, result|
|
||||||
result << row.last if row[index] == val
|
result << row.last if row[index] == val
|
||||||
end
|
end
|
||||||
|
|
||||||
sum + ((classification.size.to_f / data.size) * classification.entropy)
|
((classification.size.to_f / data.size) * classification.entropy)
|
||||||
end
|
end
|
||||||
|
|
||||||
[data.classification.entropy - remainder, index]
|
[data.classification.entropy - remainder, index]
|
||||||
|
|||||||
Reference in New Issue
Block a user