scoruby 0.2.0 → 0.2.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.
- checksums.yaml +4 -4
- data/lib/random_forest/compound_predicate.rb +37 -0
- data/lib/random_forest/decision.rb +15 -0
- data/lib/random_forest/decision_tree.rb +1 -1
- data/lib/random_forest/gbm.rb +1 -1
- data/lib/random_forest/node.rb +16 -6
- data/lib/random_forest/predicate_factory.rb +35 -0
- data/lib/random_forest/random_forest.rb +1 -1
- data/lib/scoruby/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e090c4502c064f07dc851a28873a6e60bbd7a301
|
4
|
+
data.tar.gz: 3ef721448831698663309efa187103971ebe8ce1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da34ae5f3501a5f73cf28c43bd4d736f07fda38988c0f4c4504c8418a4f9e990e9a97eb94afbd2af405001252100c3b96ab43c1a61f102083b19f5725aa90714
|
7
|
+
data.tar.gz: e038681f58084d1c80998d4d6075969917072c6e45b52c67427311e9b304e85a9f9d8d560e99d20be3e0045d89ce2afcdee0bd37509dc9b446caff998b06fa30
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class CompoundPredicate
|
2
|
+
# TODO: spec
|
3
|
+
|
4
|
+
attr_reader :field
|
5
|
+
|
6
|
+
def initialize(pred_xml)
|
7
|
+
attributes = pred_xml.attributes
|
8
|
+
children = pred_xml.children
|
9
|
+
|
10
|
+
@boolean_operator = attributes['booleanOperator'].value
|
11
|
+
@predicates = []
|
12
|
+
@predicates << PredicateFactory.for(children[0])
|
13
|
+
@predicates << PredicateFactory.for(children[1])
|
14
|
+
@field = @predicates.map(&:field)
|
15
|
+
end
|
16
|
+
|
17
|
+
def true?(features)
|
18
|
+
return surrogate?(features) if @boolean_operator == 'surrogate'
|
19
|
+
return or?(features) if @boolean_operator == 'or'
|
20
|
+
and?(features) if @boolean_operator == 'and'
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def surrogate?(features)
|
26
|
+
# TODO: return 1 if 0 is missing
|
27
|
+
@predicates[0].true?(features)
|
28
|
+
end
|
29
|
+
|
30
|
+
def or?(features)
|
31
|
+
@predicates.any? { |p| p.true?(features) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def and?(features)
|
35
|
+
@predicates.all? { |p| p.true?(features) }
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Decision
|
2
|
+
|
3
|
+
attr_reader :score, :score_distribution
|
4
|
+
|
5
|
+
def initialize(score, score_distributions)
|
6
|
+
@score = score
|
7
|
+
return if score_distributions.empty?
|
8
|
+
|
9
|
+
@score_distribution = {}
|
10
|
+
score_distributions.each { |score_distribution|
|
11
|
+
attributes = score_distribution.attributes
|
12
|
+
@score_distribution[attributes['value'].to_s] = attributes['probability'].to_s
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|
data/lib/random_forest/gbm.rb
CHANGED
@@ -19,7 +19,7 @@ class Gbm
|
|
19
19
|
def score(features)
|
20
20
|
formatted_features = Features.new(features).formatted
|
21
21
|
x = @decision_trees.map { |dt|
|
22
|
-
score = dt.decide(formatted_features)
|
22
|
+
score = dt.decide(formatted_features).score
|
23
23
|
score.to_s.to_f
|
24
24
|
}.reduce(:+) + @const
|
25
25
|
Math.exp(x) / (1 + Math.exp(x))
|
data/lib/random_forest/node.rb
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'predicate_factory'
|
2
|
+
require 'decision'
|
3
3
|
|
4
4
|
class Node
|
5
5
|
|
6
|
-
attr_reader :decision, :pred, :children
|
6
|
+
attr_reader :decision, :pred, :children
|
7
7
|
|
8
8
|
def initialize(xml)
|
9
9
|
children = xml.children
|
10
|
+
|
11
|
+
@decision = Decision.new(xml.attribute('score').to_s,
|
12
|
+
children.select { |c| c.name == 'ScoreDistribution' } )
|
13
|
+
|
14
|
+
children = remove_nodes(children)
|
15
|
+
|
10
16
|
pred_xml = children[0]
|
11
|
-
@pred =
|
12
|
-
@pred = SimpleSetPredicate.new(pred_xml) if pred_xml.name == 'SimpleSetPredicate'
|
17
|
+
@pred = PredicateFactory.for(pred_xml)
|
13
18
|
@children = []
|
14
|
-
@decision = xml.attribute('score').to_s
|
15
19
|
|
16
20
|
return if children.count == 1
|
17
21
|
|
@@ -23,4 +27,10 @@ class Node
|
|
23
27
|
def true?(features)
|
24
28
|
@pred.nil? || @pred.true?(features)
|
25
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def remove_nodes(children)
|
34
|
+
children.reject { |c| %w(Extension ScoreDistribution).include? c.name }
|
35
|
+
end
|
26
36
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'compound_predicate'
|
2
|
+
require 'simple_predicate'
|
3
|
+
require 'simple_set_predicate'
|
4
|
+
|
5
|
+
class PredicateFactory
|
6
|
+
|
7
|
+
def self.for(pred_xml)
|
8
|
+
return SimplePredicate.new(pred_xml) if pred_xml.name == 'SimplePredicate'
|
9
|
+
return SimpleSetPredicate.new(pred_xml) if pred_xml.name == 'SimpleSetPredicate'
|
10
|
+
return CompoundPredicate.new(pred_xml) if pred_xml.name == 'CompoundPredicate'
|
11
|
+
return TruePredicate.new if pred_xml.name == 'True'
|
12
|
+
return FalsePredicate.new if pred_xml.name == 'False'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
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
|
+
|
@@ -13,7 +13,7 @@ class RandomForest
|
|
13
13
|
def decisions_count(features)
|
14
14
|
formatted_features = Features.new(features).formatted
|
15
15
|
decisions = @decision_trees.collect { |decision_tree|
|
16
|
-
decision_tree.decide(formatted_features)
|
16
|
+
decision_tree.decide(formatted_features).score
|
17
17
|
}
|
18
18
|
decisions.inject(Hash.new(0)) { |h, e| h[e] += 1 ; h }
|
19
19
|
end
|
data/lib/scoruby/version.rb
CHANGED
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.1
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -124,10 +124,13 @@ 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
|
127
129
|
- lib/random_forest/decision_tree.rb
|
128
130
|
- lib/random_forest/features.rb
|
129
131
|
- lib/random_forest/gbm.rb
|
130
132
|
- lib/random_forest/node.rb
|
133
|
+
- lib/random_forest/predicate_factory.rb
|
131
134
|
- lib/random_forest/random_forest.rb
|
132
135
|
- lib/random_forest/simple_predicate.rb
|
133
136
|
- lib/random_forest/simple_set_predicate.rb
|