examples/decision_trees/id3_example.rb in ai4r-1.2 vs examples/decision_trees/id3_example.rb in ai4r-1.3
- old
+ new
@@ -5,28 +5,25 @@
#
# You can redistribute it and/or modify it under the terms of
# the Mozilla Public License version 1.1 as published by the
# Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
-#require File.dirname(__FILE__) + '/../../lib/decision_tree/id3'
require File.dirname(__FILE__) + '/../../lib/ai4r/classifiers/id3'
-require 'csv'
# Load data from data_set.csv
-data_set = []
-CSV::Reader.parse(File.open("#{File.dirname(__FILE__)}/data_set.csv", 'r')) do |row|
- data_set << row
-end
-data_labels = data_set.shift
+data_filename = "#{File.dirname(__FILE__)}/data_set.csv"
+data_set = Ai4r::Data::DataSet.new.load_data_and_labels_from_csv data_filename
# Build ID3 tree
-id3 = Ai4r::Classifiers::ID3.new.build(data_set, data_labels)
+id3 = Ai4r::Classifiers::ID3.new.build(data_set)
# Show rules
puts "Discovered rules are:"
-puts id3.to_s
+puts id3.get_rules
+puts
# Try to predict some values
+puts "Prediction samples:"
puts "['Moron Sur (GBA)','4','[86 m2 - 100 m2]'] => " + id3.eval(['Moron Sur (GBA)','4','[86 m2 - 100 m2]'])
puts "['Moron Sur (GBA)','3','[101 m2 - 125 m2]'] => " + id3.eval(['Moron Sur (GBA)','3','[101 m2 - 125 m2]'])
puts "['Recoleta (CABA)','3','[86 m2 - 100 m2]'] => " + id3.eval(['Recoleta (CABA)','3','[86 m2 - 100 m2]',])
puts "['Tigre (GBA)','3','[71 m2 - 85 m2]'] => " + id3.eval(['Tigre (GBA)','3','[71 m2 - 85 m2]',])