Class: SvmToolkit::Svm

Inherits:
Object
  • Object
show all
Defined in:
lib/svm_toolkit/svm.rb

Overview

Extends the Java SVM class.

Defined Under Namespace

Classes: ContourDisplay, CrossValidationSearch, SvmTrainer

Class Method Summary collapse

Class Method Details

.cross_validation(problem, parameters, n_folds, target) ⇒ Object

Using given Problem and Parameter instances, builds and trains SVM models using n-fold cross-validation.



14
15
16
# File 'lib/svm_toolkit/svm.rb', line 14

def self.cross_validation(problem, parameters, n_folds, target)
  Svm.svm_cross_validation(problem, parameters, n_folds, target)
end

.cross_validation_search(training_set, cross_valn_set, costs = [-2,-1,0,1,2,3].collect {|i| 2**i}, gammas = [-2,-1,0,1,2,3].collect {|i| 2**i}, params = {}) ⇒ Object

Perform cross validation search on given gamma/cost values, using an RBF kernel, returning the best performing model and optionally displaying a contour map of performance.

  • training_set - instance of Problem, used for training

  • cross_valn_set - instance of Problem, used for evaluating models

  • costs - array of cost values to search across

  • gammas - array of gamma values to search across

  • params - Optional parameters include:

    • :evaluator => Evaluator::OverallAccuracy, the name of the class to use for computing performance

    • :show_plot => false, whether to display contour plot

Returns an instance of Model, the best performing model.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/svm_toolkit/svm.rb', line 34

def Svm.cross_validation_search(training_set, cross_valn_set, 
                                 costs = [-2,-1,0,1,2,3].collect {|i| 2**i}, 
                                 gammas = [-2,-1,0,1,2,3].collect {|i| 2**i}, 
                                 params = {})
  evaluator = params.fetch :evaluator, Evaluator::OverallAccuracy
  show_plot = params.fetch :show_plot, false

  fjp = ForkJoinPool.new
  task = CrossValidationSearch.new gammas, costs, training_set, cross_valn_set, evaluator
  results, best_model = fjp.invoke task

  if show_plot
    ContourDisplay.new(costs.collect {|n| Math.log2(n)}, 
                       gammas.collect {|n| Math.log2(n)}, 
                       results)
  end

  return best_model
end

.train(problem, parameters) ⇒ Object

Using given Problem and Parameter instances, builds and trains an SVM model.



8
9
10
# File 'lib/svm_toolkit/svm.rb', line 8

def self.train(problem, parameters)
  Svm.svm_train(problem, parameters)
end