idhja22 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,26 @@
1
1
  module Idhja22
2
2
  class Node
3
3
  class << self
4
- def build_node(dataset, attributes_available, depth, parent_probability = nil)
4
+ def build_node(dataset, attributes_available, depth, prior = nil)
5
5
  if(dataset.size < Idhja22.config.min_dataset_size)
6
- return Idhja22::LeafNode.new(dataset.m_estimate(parent_probability), dataset.category_label)
6
+ return Idhja22::LeafNode.build(dataset, prior)
7
7
  end
8
8
 
9
9
  #if successful termination - create and return a leaf node
10
10
  if(dataset.terminating? && depth > 0) # don't terminate without splitting the data at least once
11
- return Idhja22::LeafNode.new(dataset.m_estimate(parent_probability), dataset.category_label)
11
+ return Idhja22::LeafNode.build(dataset, prior)
12
12
  end
13
13
 
14
14
  if(depth >= 3) # don't let trees get too long
15
- return Idhja22::LeafNode.new(dataset.m_estimate(parent_probability), dataset.category_label)
15
+ return Idhja22::LeafNode.build(dataset, prior)
16
16
  end
17
17
 
18
18
  #if we have no more attributes left to split the dataset on, then return a leafnode
19
19
  if(attributes_available.empty?)
20
- return Idhja22::LeafNode.new(dataset.m_estimate(parent_probability), dataset.category_label)
20
+ return Idhja22::LeafNode.build(dataset, prior)
21
21
  end
22
22
 
23
- node = DecisionNode.build(dataset, attributes_available, depth, dataset.m_estimate(parent_probability))
23
+ node = DecisionNode.build(dataset, attributes_available, depth, prior)
24
24
 
25
25
  return node
26
26
  end
@@ -55,13 +55,15 @@ module Idhja22
55
55
  attr_accessor :branches, :decision_attribute, :default_probability
56
56
 
57
57
  class << self
58
- def build(dataset, attributes_available, depth, parent_probability=nil)
58
+ def build(dataset, attributes_available, depth, prior=nil)
59
59
  data_split, best_attribute = best_attribute(dataset, attributes_available)
60
60
 
61
- output_node = new(best_attribute, parent_probability)
61
+ probability_guess = dataset.m_estimate(prior)
62
62
 
63
- data_split.each do |value, dataset|
64
- node = Node.build_node(dataset, attributes_available-[best_attribute], depth+1, dataset.m_estimate(parent_probability))
63
+ output_node = new(best_attribute, probability_guess)
64
+
65
+ data_split.each do |value, ds|
66
+ node = Node.build_node(ds, attributes_available-[best_attribute], depth+1, probability_guess)
65
67
 
66
68
  output_node.add_branch(value, node) if node && !(node.is_a?(DecisionNode) && node.branches.empty?)
67
69
  end
@@ -139,6 +141,13 @@ module Idhja22
139
141
 
140
142
  class LeafNode < Node
141
143
  attr_reader :probability, :category_label
144
+ class << self
145
+ def build(dataset, prior)
146
+ return new(dataset.m_estimate(prior), dataset.category_label)
147
+ end
148
+ end
149
+
150
+
142
151
  def initialize(probability, category_label)
143
152
  @probability = probability
144
153
  @category_label = category_label
@@ -1,3 +1,3 @@
1
1
  module Idhja22
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -21,7 +21,7 @@ describe Idhja22::Tree do
21
21
 
22
22
  describe('#get_rules') do
23
23
  it 'should list the rules of the tree' do
24
- Idhja22::Tree.train(@ds).get_rules.should == "if 2 == a and 4 == a and then chance of C = 0.88\nelsif 2 == a and 4 == b and then chance of C = 0.48\nelsif 2 == b and then chance of C = 0.38"
24
+ Idhja22::Tree.train(@ds).get_rules.should == "if 2 == a and 4 == a and then chance of C = 0.77\nelsif 2 == a and 4 == b and then chance of C = 0.53\nelsif 2 == b and then chance of C = 0.46"
25
25
  end
26
26
  end
27
27
 
@@ -48,7 +48,7 @@ describe Idhja22::Tree do
48
48
  it 'should return the probabilty at the leaf of the tree' do
49
49
  tree = Idhja22::Tree.train(@ds)
50
50
  query = Idhja22::Dataset::Datum.new(['z','z','a','z','a'],['0', '1','2','3','4'],'C')
51
- tree.evaluate(query).should be_within(0.001).of(0.878)
51
+ tree.evaluate(query).should be_within(0.001).of(0.774)
52
52
  end
53
53
  end
54
54
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Idhja22 do
4
4
  describe 'VERSION' do
5
5
  it 'should be current version' do
6
- Idhja22::VERSION.should == '2.0.0'
6
+ Idhja22::VERSION.should == '2.0.1'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idhja22
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-21 00:00:00.000000000 Z
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -177,7 +177,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  segments:
179
179
  - 0
180
- hash: -4074205080648422327
180
+ hash: 3616736987678555050
181
181
  required_rubygems_version: !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements:
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  version: '0'
187
187
  segments:
188
188
  - 0
189
- hash: -4074205080648422327
189
+ hash: 3616736987678555050
190
190
  requirements: []
191
191
  rubyforge_project:
192
192
  rubygems_version: 1.8.24