Class: Liblinear::CrossValidator

Inherits:
Object
  • Object
show all
Includes:
Liblinear, Liblinearswig
Defined in:
lib/liblinear/cross_validator.rb

Constant Summary

Constants included from Liblinear

L1R_L2LOSS_SVC, L1R_LR, L2R_L1LOSS_SVC_DUAL, L2R_L1LOSS_SVR_DUAL, L2R_L2LOSS_SVC, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVR, L2R_L2LOSS_SVR_DUAL, L2R_LR, L2R_LR_DUAL, MCSVM_CS, VERSION

Instance Method Summary collapse

Methods included from Liblinear

#array_to_hash, #convert_to_feature_node_array, #double_array_c_to_ruby, #free_double_array, #free_int_array, #int_array_c_to_ruby, #max_index, #new_double_array, #new_int_array

Constructor Details

#initialize(prob, param, fold) ⇒ CrossValidator

Returns a new instance of CrossValidator.

Parameters:



9
10
11
12
13
# File 'lib/liblinear/cross_validator.rb', line 9

def initialize(prob, param, fold)
  @prob = prob
  @param = param
  @fold = fold
end

Instance Method Details

#accuracyDouble

Returns:

  • (Double)


23
24
25
26
27
28
29
# File 'lib/liblinear/cross_validator.rb', line 23

def accuracy
  total_correct = 0
  @prob.labels.size.times do |i|
    total_correct += 1 if @predictions[i] == @prob.labels[i].to_f
  end
  total_correct.to_f / @prob.labels.size.to_f
end

#executeArray <Integer, Double>

Returns:

  • (Array <Integer, Double>)


16
17
18
19
20
# File 'lib/liblinear/cross_validator.rb', line 16

def execute
  target = new_double_array(@prob.labels.size.times.map { 0.0 })
  cross_validation(@prob.prob, @param.param, @fold, target)
  @predictions = double_array_c_to_ruby(target, @prob.labels.size)
end

#mean_squared_errorDouble

Returns:

  • (Double)


32
33
34
35
36
37
38
# File 'lib/liblinear/cross_validator.rb', line 32

def mean_squared_error
  total_error = 0.0
  @prob.labels.size.times do |i|
    total_error += (@prob.labels[i].to_f - @predictions[i].to_f) ** 2
  end
  total_error / @prob.labels.size.to_f
end

#squared_correlation_coefficientDouble

Returns:

  • (Double)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/liblinear/cross_validator.rb', line 41

def squared_correlation_coefficient
  sum_x = 0.0
  sum_y = 0.0
  sum_xx = 0.0
  sum_yy = 0.0
  sum_xy = 0.0
  @prob.labels.size.times do |i|
    sum_x += @predictions[i].to_f
    sum_y += @prob.labels[i].to_f
    sum_xx += @predictions[i].to_f ** 2
    sum_yy += @prob.labels[i].to_f ** 2
    sum_xy += @predictions[i].to_f * @prob.labels[i].to_f
  end
  ((@prob.labels.size * sum_xy - sum_x * sum_y) ** 2) /
    ((@prob.labels.size * sum_xx - sum_x ** 2) * (@prob.labels.size * sum_yy - sum_y ** 2))
end