idhja22 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,13 +52,13 @@ module Idhja22
52
52
  end
53
53
 
54
54
  class DecisionNode < Node
55
- attr_reader :branches, :decision_attribute
55
+ attr_accessor :branches, :decision_attribute, :default_probability
56
56
 
57
57
  class << self
58
58
  def build(dataset, attributes_available, depth, parent_probability=nil)
59
59
  data_split, best_attribute = best_attribute(dataset, attributes_available)
60
60
 
61
- output_node = new(best_attribute)
61
+ output_node = new(best_attribute, parent_probability)
62
62
 
63
63
  data_split.each do |value, dataset|
64
64
  node = Node.build_node(dataset, attributes_available-[best_attribute], depth+1, dataset.m_estimate(parent_probability))
@@ -72,9 +72,10 @@ module Idhja22
72
72
  end
73
73
  end
74
74
 
75
- def initialize(decision_attribute)
75
+ def initialize(decision_attribute, default_probability = nil)
76
76
  @decision_attribute = decision_attribute
77
77
  @branches = {}
78
+ @default_probability = (default_probability || Idhja22.config.default_probability)
78
79
  end
79
80
 
80
81
  def add_branch(attr_value, node)
@@ -108,7 +109,7 @@ module Idhja22
108
109
  def evaluate(query)
109
110
  queried_value = query[self.decision_attribute]
110
111
  branch = self.branches[queried_value]
111
- raise Idhja22::Dataset::Datum::UnknownAttributeValue, "when looking at attribute labelled #{self.decision_attribute} could not find branch for value #{queried_value}" if branch.nil?
112
+ return default_probability if branch.nil?
112
113
  branch.evaluate(query)
113
114
  end
114
115
 
@@ -1,3 +1,3 @@
1
1
  module Idhja22
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -94,9 +94,9 @@ describe Idhja22::DecisionNode do
94
94
  end
95
95
 
96
96
  context 'unknown attribute value' do
97
- it 'should raise an error' do
97
+ it 'should return default probability value' do
98
98
  query = Idhja22::Dataset::Datum.new(['c', 'a'], ['3', '4'], 'C')
99
- expect {@simple_decision_node.evaluate(query)}.to raise_error(Idhja22::Dataset::Datum::UnknownAttributeValue)
99
+ @simple_decision_node.evaluate(query).should == 0.5
100
100
  end
101
101
  end
102
102
  end
@@ -202,4 +202,20 @@ describe Idhja22::DecisionNode do
202
202
  end
203
203
  end
204
204
  end
205
+
206
+ describe '.new' do
207
+ it 'should set instance variables' do
208
+ dn = Idhja22::DecisionNode.new('attr_label', 0.7)
209
+ dn.decision_attribute.should == 'attr_label'
210
+ dn.default_probability.should == 0.7
211
+ dn.branches.should == {}
212
+ end
213
+
214
+ context 'without provided default probability' do
215
+ it 'should set default to config default' do
216
+ dn = Idhja22::DecisionNode.new('attr_label')
217
+ dn.default_probability.should == 0.5
218
+ end
219
+ end
220
+ end
205
221
  end
data/spec/tree_spec.rb CHANGED
@@ -70,7 +70,7 @@ describe Idhja22::Tree do
70
70
  end
71
71
 
72
72
  it 'should treat a validation example as one it will never get right' do
73
- @tree.validate(@vps).should == 0.0
73
+ @tree.validate(@vps).should == 0.55
74
74
  end
75
75
  end
76
76
  end
data/spec/version_spec.rb CHANGED
@@ -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 == '1.1.1'
6
+ Idhja22::VERSION.should == '1.2.0'
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: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -176,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
176
176
  version: '0'
177
177
  segments:
178
178
  - 0
179
- hash: -1322747474535878301
179
+ hash: 144077394884408186
180
180
  required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  none: false
182
182
  requirements:
@@ -185,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  version: '0'
186
186
  segments:
187
187
  - 0
188
- hash: -1322747474535878301
188
+ hash: 144077394884408186
189
189
  requirements: []
190
190
  rubyforge_project:
191
191
  rubygems_version: 1.8.24