Class: ThunderSVM::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/thundersvm/model.rb

Direct Known Subclasses

Classifier, Regressor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(svm_type: :c_svc, kernel: :rbf, degree: 3, gamma: nil, coef0: 0, c: 1, nu: 0.5, epsilon: 0.1, max_memory: 8192, tolerance: 0.001, probability: false, gpu: 0, cores: nil, verbose: nil) ⇒ Model

Returns a new instance of Model.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/thundersvm/model.rb', line 3

def initialize(svm_type: :c_svc, kernel: :rbf, degree: 3, gamma: nil, coef0: 0,
  c: 1, nu: 0.5, epsilon: 0.1, max_memory: 8192, tolerance: 0.001,
  probability: false, gpu: 0, cores: nil, verbose: nil)

  @svm_type = svm_type.to_sym
  @kernel = kernel.to_sym
  @degree = degree
  @gamma = gamma
  @coef0 = coef0
  @c = c
  @nu = nu
  @epsilon = epsilon
  @max_memory = max_memory
  @tolerance = tolerance
  @probability = probability
  @gpu = gpu
  @cores = cores
  @verbose = verbose
end

Class Method Details

.finalize_file(file) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/thundersvm/model.rb', line 85

def self.finalize_file(file)
  # must use proc instead of stabby lambda
  proc do
    file.close
    file.unlink
  end
end

Instance Method Details

#cv(x, y = nil, folds: 5) ⇒ Object



27
28
29
# File 'lib/thundersvm/model.rb', line 27

def cv(x, y = nil, folds: 5)
  train(x, y, folds: folds)
end

#dual_coefObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/thundersvm/model.rb', line 69

def dual_coef
  vectors = []
  sv = false
  read_txt do |line|
    if sv
      index = line.index("1:")
      line[0...index].split(" ").map(&:to_f).each_with_index do |v, i|
        (vectors[i] ||= []) << v
      end
    elsif line.start_with?("SV")
      sv = true
    end
  end
  vectors
end

#fit(x, y = nil) ⇒ Object



23
24
25
# File 'lib/thundersvm/model.rb', line 23

def fit(x, y = nil)
  train(x, y)
end

#load_model(path) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/thundersvm/model.rb', line 46

def load_model(path)
  @model_file ||= create_tempfile
  # TODO ensure tempfile is still cleaned up
  FileUtils.cp(path, @model_file.path)
  @svm_type = read_header["svm_type"].to_sym
  @kernel = read_header["kernel_type"].to_sym
  nil
end

#predict(x) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/thundersvm/model.rb', line 31

def predict(x)
  dataset_file = create_dataset(x)
  out_file = create_tempfile
  argv = ["thundersvm-predict", dataset_file.path, @model_file.path, out_file.path]
  FFI.thundersvm_predict(argv.size, str_ptr(argv))
  func = [:c_svc, :nu_svc].include?(@svm_type) ? :to_i : :to_f
  out_file.each_line.map(&func)
end

#save_model(path) ⇒ Object

Raises:



40
41
42
43
44
# File 'lib/thundersvm/model.rb', line 40

def save_model(path)
  raise Error, "Not trained" unless @model_file
  FileUtils.cp(@model_file.path, path)
  nil
end

#support_vectorsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/thundersvm/model.rb', line 55

def support_vectors
  vectors = []
  sv = false
  read_txt do |line|
    if sv
      index = line.index("1:")
      vectors << line[index..-1].split(" ").map { |v| v.split(":").last.to_f }
    elsif line.start_with?("SV")
      sv = true
    end
  end
  vectors
end