-
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.
Separate array and object extensions from id3_tree
- Loading branch information
Danielius
committed
Nov 20, 2015
1 parent
6adbebb
commit 9ee1fdd
Showing
4 changed files
with
36 additions
and
11 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Object | ||
def save_to_file(filename) | ||
File.open(filename, 'w+') { |f| f << Marshal.dump(self) } | ||
end | ||
|
||
def self.load_from_file(filename) | ||
Marshal.load(File.read(filename)) | ||
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,2 +1,3 @@ | ||
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb' | ||
require 'core_extension/array.rb' | ||
require 'core_extensions/object' | ||
require 'core_extensions/array' |
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