Top Level Namespace

Includes:
Libsvm

Defined Under Namespace

Classes: Model, Parameter, Problem

Instance Method Summary collapse

Instance Method Details

#_convert_to_svm_node_array(x) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/svm.rb', line 105

def _convert_to_svm_node_array(x)
  # convert a hash or array to an svm_node array
  
  # Find non zero elements
  iter_range = []
  if x.class == Hash
    x.each {|k, v|
      # all zeros kept due to the precomputed kernel; no good solution yet     
      iter_range << k  # if v != 0
    }
  elsif x.class == Array
    x.each_index {|j| 
      iter_range << j #if x[j] != 0
    }
  else
    raise TypeError,"data must be a hash or an array"
  end
  
  iter_range.sort!
  data = svm_node_array(iter_range.size+1)
  svm_node_array_set(data,iter_range.size,-1,0)
  
  j = 0
  for k in iter_range
    svm_node_array_set(data,j,k,x[k])
    j = j + 1
  end
  return data
end

#_double_array(seq) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/svm.rb', line 15

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



44
45
46
47
48
# File 'lib/svm.rb', line 44

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



32
33
34
35
36
# File 'lib/svm.rb', line 32

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

#_free_int_array(x) ⇒ Object



26
27
28
29
30
# File 'lib/svm.rb', line 26

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

#_int_array(seq) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/svm.rb', line 4

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



38
39
40
41
42
# File 'lib/svm.rb', line 38

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



323
324
325
326
327
328
329
330
331
332
# File 'lib/svm.rb', line 323

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/svm.rb', line 334

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