Class: Treat::Workers::Learners::Classifiers::Linear

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/workers/learners/classifiers/linear.rb

Constant Summary collapse

DefaultOptions =
{
  bias: 1,
  eps: 0.1,
  solver_type: MCSVM_CS
}
@@classifiers =
{}

Class Method Summary collapse

Class Method Details

.array_to_hash(array) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/treat/workers/learners/classifiers/linear.rb', line 33

def self.array_to_hash(array)
  hash = {}
  0.upto(array.length - 1) do |i|
    hash[i] = array[i]
  end
  hash
end

.classify(entity, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/treat/workers/learners/classifiers/linear.rb', line 13

def self.classify(entity, options = {})
  options = DefaultOptions.merge(options)
  dset = options[:training]
  prob, items = dset.problem, dset.items
  if !@@classifiers[prob]
    lparam = LParameter.new
    lparam.solver_type = options[:solver_type]
    lparam.eps = options[:eps]
    lbls = items.map { |it| it[:features][-1] }
    exs = items.map { |it| it[:features][0..-2] }.
    map { |ary| self.array_to_hash(ary) }
    lprob = LProblem.new(lbls, exs, options[:bias])
    model = LModel.new(lprob, lparam)
    @@classifiers[prob] = model
  end
  features = prob.export_features(entity, false)
  @@classifiers[prob].predict(
  self.array_to_hash(features))
end