class MachineLearner::AdaBoost
AdaBoost
クラス
Public Class Methods
new(learners)
click to toggle source
コンストラクタ @param learners [Array<Learner>] 学習器のリスト
# File lib/machine_learner/adaboost.rb, line 70 def initialize(learners) @learners = learners @alphas = [] end
Public Instance Methods
classify(x)
click to toggle source
識別を行う @param x 特徴空間 @return [Fixnum] 識別結果
# File lib/machine_learner/adaboost.rb, line 108 def classify(x) classify_raw(x) > 0 ? 1 : -1 end
classify_raw(x)
click to toggle source
識別結果の生の値を返す @param x 特徴空間 @return [Float] 識別結果の生の値(-1..1)
# 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)
click to toggle source
データを元に学習を行う @param datas [Array<DataSet>] トレーニングデータの配列 @return [Array<Boolean>] 識別結果の配列
# 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()
click to toggle source
Learnerを表す文字列
# File lib/machine_learner/adaboost.rb, line 113 def to_s [@learners, @alphas].transpose.map {|l, a| "(#{a.round(3)} * #{l})" }.join(" + "); end