Class: SVM::Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg1, arg2 = nil) ⇒ Model

Returns a new instance of Model.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/libsvm/model.rb', line 5

def initialize(arg1,arg2=nil)
  if arg2 == nil
    # create model from file
    filename = arg1
    @model = svm_load_model(filename)
  else
    # create model from problem and parameter
    prob,param = arg1,arg2
    @prob = prob
    if param.gamma == 0
      param.gamma = 1.0/prob.maxlen
    end
    msg = svm_check_parameter(prob.prob,param.param)
    raise ::ArgumentError, msg if msg
    @model = svm_train(prob.prob, param.param)
  end

  #setup some classwide variables
  @nr_class = svm_get_nr_class(@model)
  @svm_type = svm_get_svm_type(@model)
  #create labels(classes)
  intarr = new_int(@nr_class)
  svm_get_labels(@model,intarr)
  @labels = _int_array_to_list(intarr, @nr_class)
  delete_int(intarr)
  #check if valid probability model
  @probability = svm_check_probability_model(@model)
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



3
4
5
# File 'lib/libsvm/model.rb', line 3

def model
  @model
end

Instance Method Details

#destroyObject



135
136
137
# File 'lib/libsvm/model.rb', line 135

def destroy
  svm_destroy_model(@model)
end

#get_labelsObject



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

def get_labels
  if @svm_type == NU_SVR or @svm_type == EPSILON_SVR or @svm_type == ONE_CLASS
    raise TypeError, "Unable to get label from a SVR/ONE_CLASS model"
  end
  return @labels
end

#get_nr_classObject



42
43
44
# File 'lib/libsvm/model.rb', line 42

def get_nr_class
  return @nr_class
end

#get_svr_pdfObject



125
126
127
128
129
# File 'lib/libsvm/model.rb', line 125

def get_svr_pdf
  #get_svr_probability will handle error checking
  sigma = get_svr_probability()
  return Proc.new{|z| exp(-z.abs/sigma)/(2*sigma)}  # TODO: verify this works
end

#get_svr_probabilityObject



116
117
118
119
120
121
122
123
# File 'lib/libsvm/model.rb', line 116

def get_svr_probability
  #leave the Error checking to svm.cpp code
  ret = svm_get_svr_probability(@model)
  if ret == 0
    raise TypeError, "not a regression model or probability information not available"
  end
  return ret
end

#predict(x, max = x.max) ⇒ Object



34
35
36
37
38
39
# File 'lib/libsvm/model.rb', line 34

def predict(x, max = x.max)
  data = SVM.convert_to_svm_node_array(x, max)
  ret = svm_predict(@model,data)
  svm_node_array_destroy(data)
  return ret
end

#predict_probability(x) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/libsvm/model.rb', line 90

def predict_probability(x)
  #c code will do nothing on wrong type, so we have to check ourself
  if @svm_type == NU_SVR or @svm_type == EPSILON_SVR
    raise TypeError, "call get_svr_probability or get_svr_pdf for probability output of regression"
  elsif @svm_type == ONE_CLASS
    raise TypeError, "probability not supported yet for one-class problem"
  end
  #only C_SVC,NU_SVC goes in
  if not @probability
    raise TypeError, "model does not support probabiliy estimates"
  end

  #convert x into svm_node, alloc a double array to receive probabilities
  data = SVM.convert_to_svm_node_array(x)
  dblarr = new_double(@nr_class)
  pred = svm_predict_probability(@model, data, dblarr)
  pv = _double_array_to_list(dblarr, @nr_class)
  delete_double(dblarr)
  svm_node_array_destroy(data)
  p = {}
  for i in (0..@labels.size-1)
    p[@labels[i]] = pv[i]
  end
  return pred, p
end

#predict_values(x) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/libsvm/model.rb', line 65

def predict_values(x)
  v=predict_values_raw(x)
  #puts v.inspect
  if @svm_type == NU_SVR or @svm_type == EPSILON_SVR or @svm_type == ONE_CLASS
    return v[0]
  else #self.svm_type == C_SVC or self.svm_type == NU_SVC
    count = 0
  
    # create a width x height array
    width = @labels.size
    height = @labels.size
    d = Array.new(width)
    d.map! { Array.new(height) }
  
    for i in (0..@labels.size-1)
      for j in (i+1..@labels.size-1)
        d[@labels[i]][@labels[j]] = v[count]
        d[@labels[j]][@labels[i]] = -v[count]
        count += 1
      end
    end
    return d
  end
end

#predict_values_raw(x) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/libsvm/model.rb', line 53

def predict_values_raw(x)
  #convert x into svm_node, allocate a double array for return
  n = (@nr_class*(@nr_class-1)/2).floor
  data = SVM.convert_to_svm_node_array(x)
  dblarr = new_double(n)
  svm_predict_values(@model, data, dblarr)
  ret = _double_array_to_list(dblarr, n)
  delete_double(dblarr)
  svm_node_array_destroy(data)
  return ret
end

#save(filename) ⇒ Object



131
132
133
# File 'lib/libsvm/model.rb', line 131

def save(filename)
  svm_save_model(filename,@model)
end