Class: MachineLearner::AdaBoost
- Inherits:
-
Learner
- Object
- Classifier
- Learner
- MachineLearner::AdaBoost
- Defined in:
- lib/machine_learner/adaboost.rb
Overview
AdaBoost クラス
Instance Method Summary collapse
-
#classify(x) ⇒ Fixnum
識別を行う.
-
#classify_raw(x) ⇒ Float
識別結果の生の値を返す.
-
#initialize(learners) ⇒ AdaBoost
constructor
コンストラクタ.
-
#learn(datas, ds = nil) ⇒ Array<Boolean>
データを元に学習を行う.
-
#to_s ⇒ Object
Learnerを表す文字列.
Methods inherited from Classifier
Constructor Details
#initialize(learners) ⇒ AdaBoost
コンストラクタ
70 71 72 73 |
# File 'lib/machine_learner/adaboost.rb', line 70 def initialize(learners) @learners = learners @alphas = [] end |
Instance Method Details
#classify(x) ⇒ Fixnum
識別を行う
108 109 110 |
# File 'lib/machine_learner/adaboost.rb', line 108 def classify(x) classify_raw(x) > 0 ? 1 : -1 end |
#classify_raw(x) ⇒ Float
識別結果の生の値を返す
101 102 103 |
# File 'lib/machine_learner/adaboost.rb', line 101 def classify_raw(x) [@learners, @alphas].transpose.reduce(0) { |score, (l, a)| score += l.classify(x) * a } end |
#learn(datas, ds = nil) ⇒ Array<Boolean>
データを元に学習を行う
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/machine_learner/adaboost.rb', line 78 def learn(datas, ds = nil) ds ||= [1.0 / datas.size] * datas.size @learners.each do |learner| # 学習記に順番に学習させる # results : トレーニングデータの正解、不正解の配列 results = learner.learn(datas, ds) # epsilon : 学習器のエラー率(失敗した学習データの重みの総和) epsilon = results.zip(ds).map{|r, w| r ? 0 : w }.inject(:+) epsilon = ds.min * 0.1 if(epsilon == 0) # alpha : 学習器の重み(エラー率epsilonが低いほど高い値を取る) alpha = Math.log((1 - epsilon) / epsilon) / 2 @alphas << alpha # 重みの更新 ds = ds.map.with_index{|w, i| w * Math.exp(alpha * (results[i] ? -1 : 1)) } z = ds.inject(&:+) ds.map!{|w| w / z} end end |
#to_s ⇒ Object
Learnerを表す文字列
113 114 115 |
# File 'lib/machine_learner/adaboost.rb', line 113 def to_s [@learners, @alphas].transpose.map {|l, a| "(#{a.round(3)} * #{l})" }.join(" + "); end |