mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-27 22:54:52 +00:00
Avoid polluting namespace
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
class Array
|
|
||||||
def entropy
|
|
||||||
each_with_object(Hash.new(0)) do |i, result|
|
|
||||||
result[i] += 1
|
|
||||||
end.values.inject(0, :+) do |count|
|
|
||||||
percentage = count.to_f / length
|
|
||||||
|
|
||||||
-percentage * Math.log2(percentage)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module ArrayClassification
|
|
||||||
refine Array do
|
|
||||||
def classification
|
|
||||||
collect(&:last)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
require "core_extensions/object"
|
require_relative "refinements/object_persistence"
|
||||||
require "core_extensions/array"
|
require_relative "refinements/array_entropy"
|
||||||
require File.dirname(__FILE__) + "/decisiontree/id3_tree.rb"
|
require_relative "refinements/array_classification"
|
||||||
|
require_relative "decisiontree/id3_tree"
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ module DecisionTree
|
|||||||
Node = Struct.new(:attribute, :threshold, :gain)
|
Node = Struct.new(:attribute, :threshold, :gain)
|
||||||
|
|
||||||
using ArrayClassification
|
using ArrayClassification
|
||||||
|
using ArrayEntropy
|
||||||
|
using ObjectPersistence
|
||||||
|
|
||||||
class ID3Tree
|
class ID3Tree
|
||||||
def initialize(attributes, data, default, type)
|
def initialize(attributes, data, default, type)
|
||||||
|
|||||||
7
lib/refinements/array_classification.rb
Normal file
7
lib/refinements/array_classification.rb
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module ArrayClassification
|
||||||
|
refine Array do
|
||||||
|
def classification
|
||||||
|
collect(&:last)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
lib/refinements/array_entropy.rb
Normal file
13
lib/refinements/array_entropy.rb
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
module ArrayEntropy
|
||||||
|
refine Array do
|
||||||
|
def entropy
|
||||||
|
each_with_object(Hash.new(0)) do |i, result|
|
||||||
|
result[i] += 1
|
||||||
|
end.values.inject(0, :+) do |count|
|
||||||
|
percentage = count.to_f / length
|
||||||
|
|
||||||
|
-percentage * Math.log2(percentage)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
11
lib/refinements/object_persistence.rb
Normal file
11
lib/refinements/object_persistence.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
module ObjectPersistence
|
||||||
|
refine Object do
|
||||||
|
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
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user