mirror of
https://github.com/dkam/decisiontree.git
synced 2025-12-28 07:04:53 +00:00
Separate array and object extensions from id3_tree
This commit is contained in:
25
lib/core_extensions/array.rb
Normal file
25
lib/core_extensions/array.rb
Normal 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
|
||||||
9
lib/core_extensions/object.rb
Normal file
9
lib/core_extensions/object.rb
Normal file
@@ -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
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb'
|
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb'
|
||||||
require 'core_extension/array.rb'
|
require 'core_extensions/object'
|
||||||
|
require 'core_extensions/array'
|
||||||
|
|||||||
@@ -3,16 +3,6 @@
|
|||||||
### Copyright (c) 2007 Ilya Grigorik <ilya AT igvita DOT com>
|
### Copyright (c) 2007 Ilya Grigorik <ilya AT igvita DOT com>
|
||||||
### Modifed at 2007 by José Ignacio Fernández <joseignacio.fernandez AT gmail DOT com>
|
### Modifed at 2007 by José Ignacio Fernández <joseignacio.fernandez AT gmail DOT com>
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
module DecisionTree
|
module DecisionTree
|
||||||
Node = Struct.new(:attribute, :threshold, :gain)
|
Node = Struct.new(:attribute, :threshold, :gain)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user