Class: MachineLearner::BayesLearner

Inherits:
Learner show all
Defined in:
lib/machine_learner/bayes.rb

Overview

ナイブベイズ学習器

Instance Method Summary collapse

Methods inherited from Classifier

#evaluate, #test

Constructor Details

#initializeBayesLearner

コンストラクタ



12
13
# File 'lib/machine_learner/bayes.rb', line 12

def initialize
end

Instance Method Details

#classify(xs) ⇒ Fixnum

識別を行う

Parameters:

  • 特徴空間

Returns:

  • 識別結果



33
34
35
36
# File 'lib/machine_learner/bayes.rb', line 33

def classify(xs)
  # 最も尤度の高い候補 y を探す
  classify_raw(xs).max{ |x, y| x[1] <=> y[1] }[0]
end

#classify_raw(xs) ⇒ Object



38
39
40
# File 'lib/machine_learner/bayes.rb', line 38

def classify_raw(xs)
  candidate_y.map{|y| [y, p_y_x(y, xs)]}.to_h
end

#learn(datas, ds = nil) ⇒ Array<Boolean>

データを元に学習を行う

Parameters:

  • トレーニングデータの配列

Returns:

  • 識別結果の配列



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/machine_learner/bayes.rb', line 18

def learn(datas, ds = nil)
  # 学習データ
  @training = datas

  # 学習データがHashか
  @hash_mode = @training[0].x.kind_of?(Hash)

  # 学習データの結果のリスト
  @ys = @training.map{|data| data.y }
  @candidate_x = @hash_mode ? {} : []
end