mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-28 07:04:53 +00:00
Avoid polluting namespace
This commit is contained in:
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