Separate array and object extensions from id3_tree

This commit is contained in:
Danielius
2015-11-20 14:39:50 +02:00
parent 6adbebbe09
commit 9ee1fdd0ee
4 changed files with 36 additions and 11 deletions

View File

@@ -0,0 +1,25 @@
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