scoruby 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e090c4502c064f07dc851a28873a6e60bbd7a301
4
- data.tar.gz: 3ef721448831698663309efa187103971ebe8ce1
3
+ metadata.gz: 29d319117ac389447c955bcd448235865fd72104
4
+ data.tar.gz: 851feb27437019ef236b7ab8130c52800f3239a4
5
5
  SHA512:
6
- metadata.gz: da34ae5f3501a5f73cf28c43bd4d736f07fda38988c0f4c4504c8418a4f9e990e9a97eb94afbd2af405001252100c3b96ab43c1a61f102083b19f5725aa90714
7
- data.tar.gz: e038681f58084d1c80998d4d6075969917072c6e45b52c67427311e9b304e85a9f9d8d560e99d20be3e0045d89ce2afcdee0bd37509dc9b446caff998b06fa30
6
+ metadata.gz: e13cff01a0cf6800aa30c522dbb27bee0cfa931f55c7aa466811f7a46aff6fc865d9d9cdcacd2ed462b334cd6e38078c6e92a5235287cadd62383ae8abf1ff22
7
+ data.tar.gz: b8664649eeef47a290f0c8547b486896d0044b3803580639468d41f8f1098e27c4de8b5f0df06a8807463e39c8d827e101fac4457de1ef3dcee947887404fe37
data/README.md CHANGED
@@ -140,6 +140,28 @@ gbm.score(features)
140
140
 
141
141
  ```
142
142
 
143
+ ### Decision Tree
144
+
145
+ #### Classify by PMML - Ruby
146
+
147
+ ```ruby
148
+ decision_tree = Scoruby.get_model 'decision_tree.pmml'
149
+
150
+ features = {
151
+ Sex: 'male',
152
+ Parch: 0,
153
+ Age: 30,
154
+ Fare: 9.6875,
155
+ Pclass: 2,
156
+ SibSp: 0,
157
+ Embarked: 'Q'
158
+ }
159
+
160
+ decision_tree.decide(features)
161
+
162
+ => #<Decision:0x007fc232384180 @score="0", @score_distribution={"0"=>"0.999615579933873", "1"=>"0.000384420066126561"}>
163
+ ```
164
+
143
165
  ## Development
144
166
 
145
167
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,4 +1,4 @@
1
- require 'decision_tree'
1
+ require 'models/decision_tree'
2
2
  require 'features'
3
3
 
4
4
  class Gbm
@@ -1,4 +1,4 @@
1
- require 'decision_tree'
1
+ require 'models/decision_tree'
2
2
 
3
3
  class RandomForest
4
4
  RF_FOREST_XPATH = 'PMML/MiningModel/Segmentation/Segment'
@@ -0,0 +1,28 @@
1
+ require 'models/random_forest'
2
+ require 'models/gbm'
3
+
4
+ class ModelsFactory
5
+ RANDOM_FOREST_MODEL = 'randomForest_Model'
6
+ GBM_INDICATION = '//OutputField[@name="scaledGbmValue"]'
7
+ MODEL_NOT_SUPPORTED_ERROR = 'model not supported'
8
+
9
+ def self.factory_for(xml)
10
+ return RandomForest.new(xml) if random_forest?(xml)
11
+ return Gbm.new(xml) if gbm?(xml)
12
+ return DecisionTree.new(xml.child) if decision_tree?(xml)
13
+
14
+ raise MODEL_NOT_SUPPORTED_ERROR
15
+ end
16
+
17
+ def self.decision_tree?(xml)
18
+ !xml.xpath('PMML/TreeModel').empty?
19
+ end
20
+
21
+ def self.random_forest?(xml)
22
+ xml.xpath('PMML/MiningModel/@modelName').to_s == RANDOM_FOREST_MODEL
23
+ end
24
+
25
+ def self.gbm?(xml)
26
+ !xml.xpath(GBM_INDICATION).empty?
27
+ end
28
+ end
@@ -1,6 +1,8 @@
1
- require 'compound_predicate'
2
- require 'simple_predicate'
3
- require 'simple_set_predicate'
1
+ require 'predicates/compound_predicate'
2
+ require 'predicates/simple_predicate'
3
+ require 'predicates/simple_set_predicate'
4
+ require 'predicates/true_predicate'
5
+ require 'predicates/false_predicate'
4
6
 
5
7
  class PredicateFactory
6
8
 
@@ -13,23 +15,4 @@ class PredicateFactory
13
15
  end
14
16
  end
15
17
 
16
- class TruePredicate
17
- def field
18
- nil
19
- end
20
-
21
- def true?(_)
22
- true
23
- end
24
- end
25
-
26
- class FalsePredicate
27
- def field
28
- nil
29
- end
30
-
31
- def true?(_)
32
- false
33
- end
34
- end
35
18
 
@@ -1,5 +1,4 @@
1
1
  class CompoundPredicate
2
- # TODO: spec
3
2
 
4
3
  attr_reader :field
5
4
 
@@ -11,7 +10,7 @@ class CompoundPredicate
11
10
  @predicates = []
12
11
  @predicates << PredicateFactory.for(children[0])
13
12
  @predicates << PredicateFactory.for(children[1])
14
- @field = @predicates.map(&:field)
13
+ @field = @predicates.map(&:field).flatten.compact
15
14
  end
16
15
 
17
16
  def true?(features)
@@ -20,10 +19,14 @@ class CompoundPredicate
20
19
  and?(features) if @boolean_operator == 'and'
21
20
  end
22
21
 
22
+ def is_missing?(features)
23
+ @field.any? { |f| !features.keys.include?(f) }
24
+ end
25
+
23
26
  private
24
27
 
25
28
  def surrogate?(features)
26
- # TODO: return 1 if 0 is missing
29
+ return @predicates[1].true?(features) if @predicates[0].is_missing?(features)
27
30
  @predicates[0].true?(features)
28
31
  end
29
32
 
@@ -0,0 +1,13 @@
1
+ class FalsePredicate
2
+ def field
3
+ nil
4
+ end
5
+
6
+ def true?(_)
7
+ false
8
+ end
9
+
10
+ def is_missing?(_)
11
+ false
12
+ end
13
+ end
@@ -25,6 +25,12 @@ class SimplePredicate
25
25
  features[field].nil? || !features.has_key?(field) if @operator == IS_MISSING
26
26
  end
27
27
 
28
+ def is_missing?(features)
29
+ !features.keys.include?(@field)
30
+ end
31
+
32
+ private
33
+
28
34
  def num_true?(features)
29
35
  return false unless features[@field]
30
36
  curr_value = Float(features[@field])
@@ -15,6 +15,10 @@ class SimpleSetPredicate
15
15
  @array.include? features[@field] if @operator == IS_IN
16
16
  end
17
17
 
18
+ def is_missing?(features)
19
+ !features.keys.include?(@field)
20
+ end
21
+
18
22
  private
19
23
 
20
24
  def single_or_quoted_words(string)
@@ -0,0 +1,13 @@
1
+ class TruePredicate
2
+ def field
3
+ nil
4
+ end
5
+
6
+ def true?(_)
7
+ true
8
+ end
9
+
10
+ def is_missing?(_)
11
+ false
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Scoruby
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
data/lib/scoruby.rb CHANGED
@@ -1,15 +1,10 @@
1
1
  require 'scoruby/version'
2
+ require 'models_factory'
2
3
  require 'nokogiri'
3
- require 'random_forest'
4
- require 'gbm'
5
4
  require 'logger'
6
5
  require 'pry'
7
6
 
8
7
  module Scoruby
9
- RANDOM_FOREST_MODEL = 'randomForest_Model'
10
- GBM_INDICATION = '//OutputField[@name="scaledGbmValue"]'
11
- MODEL_NOT_SUPPORTED_ERROR = 'model not supported'
12
-
13
8
  class << self
14
9
  attr_writer :logger
15
10
 
@@ -22,13 +17,7 @@ module Scoruby
22
17
 
23
18
  def self.get_model(pmml_file_name)
24
19
  xml = xml_from_file_path(pmml_file_name)
25
- new_model(xml)
26
- end
27
-
28
- def self.new_model(xml)
29
- return RandomForest.new(xml) if random_forest?(xml)
30
- return Gbm.new(xml) if gbm?(xml)
31
- raise MODEL_NOT_SUPPORTED_ERROR
20
+ ModelsFactory.factory_for(xml)
32
21
  end
33
22
 
34
23
  def self.xml_from_file_path(pmml_file_name)
@@ -40,12 +29,4 @@ module Scoruby
40
29
  xml = Nokogiri::XML(pmml_string) { |config| config.noblanks }
41
30
  xml.remove_namespaces!
42
31
  end
43
-
44
- def self.random_forest?(xml)
45
- xml.xpath('PMML/MiningModel/@modelName').to_s == RANDOM_FOREST_MODEL
46
- end
47
-
48
- def self.gbm?(xml)
49
- !xml.xpath(GBM_INDICATION).empty?
50
- end
51
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Asaf Schers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-07 00:00:00.000000000 Z
11
+ date: 2017-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -124,16 +124,19 @@ files:
124
124
  - Rakefile
125
125
  - bin/console
126
126
  - bin/setup
127
- - lib/random_forest/compound_predicate.rb
128
- - lib/random_forest/decision.rb
129
- - lib/random_forest/decision_tree.rb
130
- - lib/random_forest/features.rb
131
- - lib/random_forest/gbm.rb
132
- - lib/random_forest/node.rb
133
- - lib/random_forest/predicate_factory.rb
134
- - lib/random_forest/random_forest.rb
135
- - lib/random_forest/simple_predicate.rb
136
- - lib/random_forest/simple_set_predicate.rb
127
+ - lib/decision.rb
128
+ - lib/features.rb
129
+ - lib/models/decision_tree.rb
130
+ - lib/models/gbm.rb
131
+ - lib/models/random_forest.rb
132
+ - lib/models_factory.rb
133
+ - lib/node.rb
134
+ - lib/predicate_factory.rb
135
+ - lib/predicates/compound_predicate.rb
136
+ - lib/predicates/false_predicate.rb
137
+ - lib/predicates/simple_predicate.rb
138
+ - lib/predicates/simple_set_predicate.rb
139
+ - lib/predicates/true_predicate.rb
137
140
  - lib/scoruby.rb
138
141
  - lib/scoruby/version.rb
139
142
  - scoruby.gemspec
File without changes
File without changes
File without changes
File without changes