-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from cheerfulstoic/master
Performance Improvements
- Loading branch information
Showing
4 changed files
with
39 additions
and
35 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
class Array | ||
def classification | ||
collect(&:last) | ||
end | ||
|
||
# calculate information entropy | ||
def entropy | ||
return 0 if empty? | ||
each_with_object(Hash.new(0)) do |i, result| | ||
result[i] += 1 | ||
end.values.sum do |count| | ||
percentage = count.to_f / length | ||
|
||
info = {} | ||
each do |i| | ||
info[i] = !info[i] ? 1 : (info[i] + 1) | ||
-percentage * Math.log2(percentage) | ||
end | ||
|
||
result(info, length) | ||
end | ||
end | ||
|
||
private | ||
|
||
def result(info, total) | ||
final = 0 | ||
info.each do |_symbol, count| | ||
next unless count > 0 | ||
percentage = count.to_f / total | ||
final += -percentage * Math.log(percentage) / Math.log(2.0) | ||
module ArrayClassification | ||
refine Array do | ||
def classification | ||
collect(&:last) | ||
end | ||
final | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb' | ||
require 'core_extensions/object' | ||
require 'core_extensions/array' | ||
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters