Top Level Namespace

Includes:
Libsvm

Defined Under Namespace

Modules: SVM

Instance Method Summary collapse

Instance Method Details

#_convert_to_svm_node_array(x) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hex-svm.rb', line 57

def _convert_to_svm_node_array(x)
  # Make index array 
  iter_range = x.each_index.to_a
  
  data = svm_node_array(iter_range.length + 1)
  svm_node_array_set(data, iter_range.length, -1, 0)
  
  iter_range.each do |k|
    svm_node_array_set(data, k, k, x[k])
  end
  
  data
end

#_double_array(seq) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/hex-svm.rb', line 22

def _double_array(seq)
  size = seq.size
  array = new_double(size)
  i = 0
  for item in seq
    double_setitem(array,i,item)
    i = i + 1
  end
  return array
end

#_double_array_to_list(x, n) ⇒ Object



51
52
53
54
55
# File 'lib/hex-svm.rb', line 51

def _double_array_to_list(x,n)
  list = []
   (0..n-1).each {|i| list << double_getitem(x,i) }
  return list
end

#_free_double_array(x) ⇒ Object



39
40
41
42
43
# File 'lib/hex-svm.rb', line 39

def _free_double_array(x)
  if !x.nil? and !x.empty?
    delete_double(x)
  end
end

#_free_int_array(x) ⇒ Object



33
34
35
36
37
# File 'lib/hex-svm.rb', line 33

def _free_int_array(x)
  if !x.nil? and !x.empty?
    delete_int(x)
  end
end

#_int_array(seq) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/hex-svm.rb', line 11

def _int_array(seq)
  size = seq.size
  array = new_int(size)
  i = 0
  for item in seq
    int_setitem(array,i,item)
    i = i + 1
  end
  return array
end

#_int_array_to_list(x, n) ⇒ Object



45
46
47
48
49
# File 'lib/hex-svm.rb', line 45

def _int_array_to_list(x,n)
  list = []
   (0..n-1).each {|i| list << int_getitem(x,i) }
  return list
end

#cross_validation(prob, param, fold) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/hex-svm.rb', line 100

def cross_validation(prob, param, fold)
  if param.gamma == 0
    param.gamma = 1.0/prob.maxlen
  end
  dblarr = new_double(prob.size)
  svm_cross_validation(prob.prob, param.param, fold, dblarr)
  ret = _double_array_to_list(dblarr, prob.size)
  delete_double(dblarr)
  return ret
end

#read_file(filename) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hex-svm.rb', line 111

def read_file filename
  labels = []
  samples = []
  max_index = 0

  f = File.open(filename)
  f.each do |line|
    elems = line.split
    sample = {}
    for e in elems[1..-1]
       points = e.split(":")
       sample[points[0].to_i] = points[1].to_f
       if points[0].to_i < max_index
          max_index = points[0].to_i
       end
    end
    labels << elems[0].to_i
    samples << sample
  end
  puts "#{filename}: #{samples.size} samples loaded."
  return labels,samples
end