Class: Treat::Workers::Learners::Classifiers::SVM
- Inherits:
-
Object
- Object
- Treat::Workers::Learners::Classifiers::SVM
- Defined in:
- lib/treat/workers/learners/classifiers/svm.rb
Constant Summary collapse
- DefaultOptions =
{ cache_size: 1, # in MB eps: 0.001, c: 10 }
- @@classifiers =
{}
Class Method Summary collapse
-
.classify(entity, options = {}) ⇒ Object
-
(Numeric) :cache_size => cache size in MB.
-
Class Method Details
.classify(entity, options = {}) ⇒ Object
-
(Numeric) :cache_size => cache size in MB.
-
(Numeric) :eps => tolerance of termination criterion
-
(Numeric) :c => C parameter
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/treat/workers/learners/classifiers/svm.rb', line 16 def self.classify(entity, = {}) = DefaultOptions.merge() dset = [:training] prob, items = dset.problem, dset.items if !@@classifiers[prob] lprob = Libsvm::Problem.new lparam = Libsvm::SvmParameter.new lparam.cache_size = [:cache_size] lparam.eps = [:eps] lparam.c = [:c] llabels = items.map { |it| it[:features][-1] } lexamples = items.map { |it| it[:features][0..-2] }. map { |ary| Libsvm::Node.features(ary) } lprob.set_examples(llabels, lexamples) model = Libsvm::Model.train(lprob, lparam) @@classifiers[prob] = model end features = prob.export_features(entity, false) @@classifiers[prob].predict( Libsvm::Node.features(features)) end |