machine_learner 0.0.2 → 0.0.3
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/Rakefile +0 -3
- data/lib/machine_learner/adaboost.rb +9 -5
- data/lib/machine_learner/bayes.rb +18 -10
- data/lib/machine_learner/dataset.rb +4 -1
- data/lib/machine_learner/version.rb +1 -1
- data/machine_learner.gemspec +0 -1
- metadata +2 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf87164a6191ea911d85afaa076ad0469fd906eb
|
4
|
+
data.tar.gz: 36ae6f7878759fd3454664dc198869bb48c52c92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3febd94592309785e96681d892321ada9889731efe76c9bfad206679a9a1ef2756b2ecf8006849cd3f9279a18a04bb11d44652bcb90f8db5787ef16052b854b
|
7
|
+
data.tar.gz: 65acb7e1618cade8704b918a777c5d79d957cb03328dd70c62ee5007e61e9a0b94cf4884370fe6826be1f0b31750b18ac2b5d9e4c764efe430ff171eb8441ad8
|
data/Rakefile
CHANGED
@@ -95,17 +95,21 @@ module MachineLearner
|
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
|
+
# 識別結果の生の値を返す
|
99
|
+
# @param x 特徴空間
|
100
|
+
# @return [Float] 識別結果の生の値(-1..1)
|
101
|
+
def classify_raw(x)
|
102
|
+
[@learners, @alphas].transpose.reduce(0) { |score, (l, a)| score += l.classify(x) * a }
|
103
|
+
end
|
104
|
+
|
98
105
|
# 識別を行う
|
99
106
|
# @param x 特徴空間
|
100
107
|
# @return [Fixnum] 識別結果
|
101
108
|
def classify(x)
|
102
|
-
|
103
|
-
score += l.classify(x) * a
|
104
|
-
}
|
105
|
-
score > 0 ? 1 : -1
|
109
|
+
classify_raw(x) > 0 ? 1 : -1
|
106
110
|
end
|
107
111
|
|
108
|
-
# Learner
|
112
|
+
# Learnerを表す文字列
|
109
113
|
def to_s
|
110
114
|
[@learners, @alphas].transpose.map {|l, a| "(#{a.round(3)} * #{l})" }.join(" + ");
|
111
115
|
end
|
@@ -8,13 +8,17 @@ module MachineLearner
|
|
8
8
|
# ナイブベイズ学習器
|
9
9
|
class BayesLearner < Learner
|
10
10
|
|
11
|
+
# コンストラクタ
|
12
|
+
def initialize
|
13
|
+
end
|
14
|
+
|
11
15
|
# データを元に学習を行う
|
12
16
|
# @param datas [Array<DataSet>] トレーニングデータの配列
|
13
17
|
# @return [Array<Boolean>] 識別結果の配列
|
14
18
|
def learn(datas, ds = nil)
|
15
19
|
@training = datas
|
16
20
|
@ys = @training.map{|data| data.y }
|
17
|
-
@
|
21
|
+
@candidate_x = []
|
18
22
|
end
|
19
23
|
|
20
24
|
# 識別を行う
|
@@ -22,24 +26,24 @@ module MachineLearner
|
|
22
26
|
# @return [Fixnum] 識別結果
|
23
27
|
def classify(xs)
|
24
28
|
# 最も尤度の高い候補 y を探す
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
return max_y
|
29
|
+
classify_raw(xs).max{ |x, y| x[1] <=> y[1] }[0]
|
30
|
+
end
|
31
|
+
|
32
|
+
def classify_raw(xs)
|
33
|
+
candidate_y.map{|y| [y, p_y_x(y, xs)]}.to_h
|
31
34
|
end
|
32
35
|
|
33
36
|
private
|
34
37
|
|
35
38
|
# P(Y | X) を計算
|
36
39
|
def p_y_x(y, xs)
|
40
|
+
# P(Y | X) = P(Y) * product(P(Xi | Y))
|
37
41
|
p = Math.log(p_y(y))
|
38
42
|
xs.each_with_index do |xi, i|
|
39
43
|
next if xi.nil?
|
40
44
|
p += Math.log(p_xi_y(xi, i, y))
|
41
45
|
end
|
42
|
-
return p
|
46
|
+
return Math.exp(p)
|
43
47
|
end
|
44
48
|
|
45
49
|
# P(Xi | Y) を計算
|
@@ -56,14 +60,18 @@ module MachineLearner
|
|
56
60
|
# P(Y) を計算
|
57
61
|
def p_y(y)
|
58
62
|
count = @ys.count(y)
|
59
|
-
return (count + 1).to_f / (@ys.size +
|
63
|
+
return (count + 1).to_f / (@ys.size + candidate_y.size + 1).to_f
|
60
64
|
end
|
61
65
|
|
62
66
|
# xの候補を列挙
|
63
67
|
def candidate_x(field)
|
64
|
-
@candidate_x ||= []
|
65
68
|
@candidate_x[field] ||= @training.map{|data| data.x[field]}.uniq.compact
|
66
69
|
end
|
70
|
+
|
71
|
+
# yの候補を列挙
|
72
|
+
def candidate_y
|
73
|
+
@candidate_y ||= @ys.uniq.compact
|
74
|
+
end
|
67
75
|
end
|
68
76
|
|
69
77
|
end
|
@@ -5,12 +5,14 @@ module MachineLearner
|
|
5
5
|
# 特徴空間xと識別結果yを持つ
|
6
6
|
class DataSet
|
7
7
|
attr_reader :x, :y
|
8
|
+
attr_accessor :id
|
8
9
|
|
9
10
|
# コンストラクタ
|
10
11
|
# @param x 特徴空間
|
11
12
|
# @param y 識別結果
|
12
|
-
def initialize(x, y)
|
13
|
+
def initialize(x, y, id = nil)
|
13
14
|
@x, @y = x, y
|
15
|
+
@id = id
|
14
16
|
end
|
15
17
|
|
16
18
|
# @return データセットを表現する文字列
|
@@ -22,3 +24,4 @@ module MachineLearner
|
|
22
24
|
end
|
23
25
|
|
24
26
|
# vim: set et ts=2 sts=2 sw=2:
|
27
|
+
|
data/machine_learner.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: machine_learner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- YutaTanaka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
description: |
|
56
42
|
This is a library for machine learning.
|
57
43
|
You can use AdaBoost and Naive Bayes easily.
|
@@ -104,4 +90,3 @@ summary: Library for machine learning.
|
|
104
90
|
test_files:
|
105
91
|
- spec/machine_learner_spec.rb
|
106
92
|
- spec/spec_helper.rb
|
107
|
-
has_rdoc:
|