Skip to content

Commit 15394f8

Browse files
committed
Merge branch 'dvisockas-master'
2 parents eaa9a2f + 2155771 commit 15394f8

File tree

7 files changed

+229
-132
lines changed

7 files changed

+229
-132
lines changed

examples/continuous-id3.rb

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
require 'decisiontree'
33
include DecisionTree
44

5-
# ---Continuous-----------------------------------------------------------------------------------------
5+
# ---Continuous---
66

77
# Read in the training data
8-
training, attributes = [], nil
9-
File.open('data/continuous-training.txt','r').each_line { |line|
10-
data = line.strip.chomp('.').split(',')
8+
training = []
9+
File.open('data/continuous-training.txt', 'r').each_line do |line|
10+
data = line.strip.chomp('.').split(',')
1111
attributes ||= data
12-
training.push(data.collect {|v| (v == 'healthy') || (v == 'colic') ? (v == 'healthy' ? 1 : 0) : v.to_f})
13-
}
12+
training_data = data.collect do |v|
13+
case v
14+
when 'healthy'
15+
1
16+
when 'colic'
17+
0
18+
else
19+
v.to_f
20+
end
21+
end
22+
training.push(training_data)
23+
end
1424

1525
# Remove the attribute row from the training data
1626
training.shift
@@ -19,15 +29,25 @@
1929
dec_tree = ID3Tree.new(attributes, training, 1, :continuous)
2030
dec_tree.train
2131

22-
#---- Test the tree....
32+
# ---Test the tree---
2333

2434
# Read in the test cases
25-
# Note: omit the attribute line (first line), we know the labels from the training data
35+
# Note: omit the attribute line (first line), we know the labels from the training data
2636
test = []
27-
File.open('data/continuous-test.txt','r').each_line { |line|
28-
data = line.strip.chomp('.').split(',')
29-
test.push(data.collect {|v| (v == 'healthy') || (v == 'colic') ? (v == 'healthy' ? 1 : 0) : v.to_f})
30-
}
37+
File.open('data/continuous-test.txt', 'r').each_line do |line|
38+
data = line.strip.chomp('.').split(',')
39+
test_data = data.collect do |v|
40+
if v == 'healthy' || v == 'colic'
41+
v == 'healthy' ? 1 : 0
42+
else
43+
v.to_f
44+
end
45+
end
46+
test.push(test_data)
47+
end
3148

3249
# Let the tree predict the output and compare it to the true specified value
33-
test.each { |t| predict = dec_tree.predict(t); puts "Predict: #{predict} ... True: #{t.last}"}
50+
test.each do |t|
51+
predict = dec_tree.predict(t)
52+
puts "Predict: #{predict} ... True: #{t.last}"
53+
end

examples/discrete-id3.rb

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
require 'rubygems'
22
require 'decisiontree'
33

4-
# ---Discrete-----------------------------------------------------------------------------------------
4+
# ---Discrete---
55

66
# Read in the training data
7-
training, attributes = [], nil
8-
File.open('data/discrete-training.txt','r').each_line { |line|
7+
training = []
8+
File.open('data/discrete-training.txt', 'r').each_line do |line|
99
data = line.strip.split(',')
1010
attributes ||= data
11-
training.push(data.collect {|v| (v == 'will buy') || (v == "won't buy") ? (v == 'will buy' ? 1 : 0) : v})
12-
}
11+
training_data = data.collect do |v|
12+
case v
13+
when 'will buy'
14+
1
15+
when "won't buy"
16+
0
17+
else
18+
v
19+
end
20+
end
21+
training.push(training_data)
22+
end
1323

1424
# Remove the attribute row from the training data
1525
training.shift
@@ -18,17 +28,31 @@
1828
dec_tree = DecisionTree::ID3Tree.new(attributes, training, 1, :discrete)
1929
dec_tree.train
2030

21-
#---- Test the tree....
31+
# ---Test the tree---
2232

2333
# Read in the test cases
24-
# Note: omit the attribute line (first line), we know the labels from the training data
34+
# Note: omit the attribute line (first line), we know the labels from the training data
2535
test = []
26-
File.open('data/discrete-test.txt','r').each_line { |line| data = line.strip.split(',')
27-
test.push(data.collect {|v| (v == 'will buy') || (v == "won't buy") ? (v == 'will buy' ? 1 : 0) : v})
28-
}
36+
File.open('data/discrete-test.txt', 'r').each_line do |line|
37+
data = line.strip.split(',')
38+
test_data = data.collect do |v|
39+
case v
40+
when 'will buy'
41+
1
42+
when "won't buy"
43+
0
44+
else
45+
v
46+
end
47+
end
48+
training.push(test_data)
49+
end
2950

3051
# Let the tree predict the output and compare it to the true specified value
31-
test.each { |t| predict = dec_tree.predict(t); puts "Predict: #{predict} ... True: #{t.last}"; }
52+
test.each do |t|
53+
predict = dec_tree.predict(t)
54+
puts "Predict: #{predict} ... True: #{t.last}"
55+
end
3256

3357
# Graph the tree, save to 'discrete.png'
34-
dec_tree.graph("discrete")
58+
dec_tree.graph('discrete')

examples/simple.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22

33
require 'rubygems'
44
require 'decisiontree'
5-
5+
66
attributes = ['Temperature']
77
training = [
88
[36.6, 'healthy'],
99
[37, 'sick'],
1010
[38, 'sick'],
1111
[36.7, 'healthy'],
1212
[40, 'sick'],
13-
[50, 'really sick'],
13+
[50, 'really sick']
1414
]
15-
15+
1616
# Instantiate the tree, and train it based on the data (set default to '1')
1717
dec_tree = DecisionTree::ID3Tree.new(attributes, training, 'sick', :continuous)
1818
dec_tree.train
1919

2020
test = [37, 'sick']
21-
22-
decision = dec_tree.predict(test)
23-
puts "Predicted: #{decision} ... True decision: #{test.last}";
24-
25-
# Graph the tree, save to 'tree.png'
26-
dec_tree.graph("tree")
2721

22+
decision = dec_tree.predict(test)
23+
puts "Predicted: #{decision} ... True decision: #{test.last}"
2824

25+
# Graph the tree, save to 'tree.png'
26+
dec_tree.graph('tree')

lib/core_extensions/array.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Array
2+
def classification
3+
collect(&:last)
4+
end
5+
6+
# calculate information entropy
7+
def entropy
8+
return 0 if empty?
9+
10+
info = {}
11+
each do |i|
12+
info[i] = !info[i] ? 1 : (info[i] + 1)
13+
end
14+
15+
result(info, length)
16+
end
17+
18+
private
19+
20+
def result(info, total)
21+
final = 0
22+
info.each do |_symbol, count|
23+
next unless count > 0
24+
percentage = count.to_f / total
25+
final += -percentage * Math.log(percentage) / Math.log(2.0)
26+
end
27+
final
28+
end
29+
end

lib/core_extensions/object.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Object
2+
def save_to_file(filename)
3+
File.open(filename, 'w+') { |f| f << Marshal.dump(self) }
4+
end
5+
6+
def self.load_from_file(filename)
7+
Marshal.load(File.read(filename))
8+
end
9+
end

lib/decisiontree.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
require File.dirname(__FILE__) + '/decisiontree/id3_tree.rb'
2+
require 'core_extensions/object'
3+
require 'core_extensions/array'

0 commit comments

Comments
 (0)