scoruby 0.2.1 → 0.2.2
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.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/lib/{random_forest → models}/gbm.rb +1 -1
- data/lib/{random_forest → models}/random_forest.rb +1 -1
- data/lib/models_factory.rb +28 -0
- data/lib/{random_forest/predicate_factory.rb → predicate_factory.rb} +5 -22
- data/lib/{random_forest → predicates}/compound_predicate.rb +6 -3
- data/lib/predicates/false_predicate.rb +13 -0
- data/lib/{random_forest → predicates}/simple_predicate.rb +6 -0
- data/lib/{random_forest → predicates}/simple_set_predicate.rb +4 -0
- data/lib/predicates/true_predicate.rb +13 -0
- data/lib/scoruby/version.rb +1 -1
- data/lib/scoruby.rb +2 -21
- metadata +15 -12
- /data/lib/{random_forest/decision.rb → decision.rb} +0 -0
- /data/lib/{random_forest/features.rb → features.rb} +0 -0
- /data/lib/{random_forest → models}/decision_tree.rb +0 -0
- /data/lib/{random_forest/node.rb → node.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29d319117ac389447c955bcd448235865fd72104
|
4
|
+
data.tar.gz: 851feb27437019ef236b7ab8130c52800f3239a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
@@ -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
|
-
|
29
|
+
return @predicates[1].true?(features) if @predicates[0].is_missing?(features)
|
27
30
|
@predicates[0].true?(features)
|
28
31
|
end
|
29
32
|
|
@@ -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])
|
data/lib/scoruby/version.rb
CHANGED
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
|
-
|
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.
|
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-
|
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/
|
128
|
-
- lib/
|
129
|
-
- lib/
|
130
|
-
- lib/
|
131
|
-
- lib/random_forest
|
132
|
-
- lib/
|
133
|
-
- lib/
|
134
|
-
- lib/
|
135
|
-
- lib/
|
136
|
-
- lib/
|
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
|