Top Level Namespace

Includes:
Liblinear

Defined Under Namespace

Classes: LModel, LParameter, LProblem

Instance Method Summary collapse

Instance Method Details

#_convert_to_feature_node_array(x, maxlen, bias = -1)) ⇒ Object



102
103
104
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
134
135
136
137
138
# File 'lib/linear.rb', line 102

def _convert_to_feature_node_array(x, maxlen, bias=-1)
  # convert a sequence or mapping to an feature_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!
  if bias >=0
    data = feature_node_array(iter_range.size+2)
    #puts "bias element (#{iter_range.size},#{bias})"
    feature_node_array_set(data,iter_range.size,maxlen+1,bias)
    feature_node_array_set(data,iter_range.size+1,-1,0)
  else
    data = feature_node_array(iter_range.size+1)
    feature_node_array_set(data,iter_range.size,-1,0)
  end
  
  j = 0
  for k in iter_range
    #puts "element #{j}= (#{k},#{x[k]})"
    feature_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/linear.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/linear.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/linear.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/linear.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/linear.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/linear.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



324
325
326
327
328
329
330
# File 'lib/linear.rb', line 324

def cross_validation(prob, param, fold)
  target = new_int(prob.size)
  Liblinear::cross_validation(prob.prob, param.param, fold, target)
  ret = _int_array_to_list(target, prob.size)
  delete_int(target)
  return ret
end

#do_cross_validation(prob_x, prob_y, param, nr_fold) ⇒ Object



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
33
34
35
36
# File 'lib/linear_cv.rb', line 5

def do_cross_validation(prob_x, prob_y, param, nr_fold)
	puts "Do cross validation for a given Linear problem."
	prob_l = prob_y.size
	total_correct = 0
	total_error = sumv = sumy = sumvv = sumyy = sumvy = 0.0
	prob = LProblem.new(prob_y, prob_x, 0)
  target = cross_validation(prob, param, nr_fold)
	for i in (0..prob_l-1)
		if false
			v = target[i]
			y = prob_y[i]
			sumv = sumv + v
			sumy = sumy + y
			sumvv = sumvv + v * v
			sumyy = sumyy + y * y
			sumvy = sumvy + v * y
			total_error = total_error + (v-y) * (v-y)
		else
			v = target[i]
			if v == prob_y[i]
				total_correct = total_correct + 1 
      end
    end
  end

	if false
		puts "Cross Validation Mean squared error = #{total_error / prob_l}"
		puts "Cross Validation Squared correlation coefficient = #{((prob_l * sumvy - sumv * sumy) * (prob_l * sumvy - sumv * sumy)) / ((prob_l * sumvv - sumv * sumv) * (prob_l * sumyy - sumy * sumy))}"
	else
		puts "Cross Validation Accuracy = #{100.0 * total_correct / prob_l}%"
  end
end

#read_file(filename) ⇒ Object



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

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
  #print elems[0].to_i
  #print " - "
  #puts sample.inspect
  end
  puts "#{filename}: #{samples.size} samples loaded."
  return labels,samples
end