Skip to content

Commit 13aed0b

Browse files
committed
Simplify with #sum
1 parent 868ed91 commit 13aed0b

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

lib/.DS_Store

6 KB
Binary file not shown.

lib/core_extensions/array.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ class Array
22
def entropy
33
each_with_object(Hash.new(0)) do |i, result|
44
result[i] += 1
5-
end.values.inject(0) do |sum, count|
5+
end.values.sum do |count|
66
percentage = count.to_f / length
7-
sum + -percentage * Math.log2(percentage)
7+
8+
-percentage * Math.log2(percentage)
89
end
910
end
1011
end

lib/decisiontree/id3_tree.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ def id3_discrete(data, attributes, attribute)
120120
index = attributes.index(attribute)
121121

122122
values = data.map { |row| row[index] }.uniq
123-
remainder = values.sort.inject(0) do |sum, val|
123+
remainder = values.sort.sum do |val|
124124
classification = data.each_with_object([]) do |row, result|
125125
result << row.last if row[index] == val
126126
end
127127

128-
sum + ((classification.size.to_f / data.size) * classification.entropy)
128+
((classification.size.to_f / data.size) * classification.entropy)
129129
end
130130

131131
[data.classification.entropy - remainder, index]

0 commit comments

Comments
 (0)