Style examples

This commit is contained in:
Danielius
2015-11-22 16:44:44 +02:00
parent 9293375bda
commit 2155771683
3 changed files with 76 additions and 34 deletions

View File

@@ -2,15 +2,25 @@ require 'rubygems'
require 'decisiontree'
include DecisionTree
# ---Continuous-----------------------------------------------------------------------------------------
# ---Continuous---
# Read in the training data
training, attributes = [], nil
File.open('data/continuous-training.txt','r').each_line { |line|
data = line.strip.chomp('.').split(',')
training = []
File.open('data/continuous-training.txt', 'r').each_line do |line|
data = line.strip.chomp('.').split(',')
attributes ||= data
training.push(data.collect {|v| (v == 'healthy') || (v == 'colic') ? (v == 'healthy' ? 1 : 0) : v.to_f})
}
training_data = data.collect do |v|
case v
when 'healthy'
1
when 'colic'
0
else
v.to_f
end
end
training.push(training_data)
end
# Remove the attribute row from the training data
training.shift
@@ -19,15 +29,25 @@ training.shift
dec_tree = ID3Tree.new(attributes, training, 1, :continuous)
dec_tree.train
#---- Test the tree....
# ---Test the tree---
# Read in the test cases
# Note: omit the attribute line (first line), we know the labels from the training data
# Note: omit the attribute line (first line), we know the labels from the training data
test = []
File.open('data/continuous-test.txt','r').each_line { |line|
data = line.strip.chomp('.').split(',')
test.push(data.collect {|v| (v == 'healthy') || (v == 'colic') ? (v == 'healthy' ? 1 : 0) : v.to_f})
}
File.open('data/continuous-test.txt', 'r').each_line do |line|
data = line.strip.chomp('.').split(',')
test_data = data.collect do |v|
if v == 'healthy' || v == 'colic'
v == 'healthy' ? 1 : 0
else
v.to_f
end
end
test.push(test_data)
end
# Let the tree predict the output and compare it to the true specified value
test.each { |t| predict = dec_tree.predict(t); puts "Predict: #{predict} ... True: #{t.last}"}
test.each do |t|
predict = dec_tree.predict(t)
puts "Predict: #{predict} ... True: #{t.last}"
end