Class: Liblinear::Problem

Inherits:
Object
  • Object
show all
Includes:
Liblinear, Liblinearswig
Defined in:
lib/liblinear/problem.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 Attribute Summary collapse

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(labels, examples, bias = -1)) ⇒ Problem

Returns a new instance of Problem.

Parameters:

  • labels (Array <Double>)
  • examples (Array <Double, Hash>)
  • bias (Double) (defaults to: -1))

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/liblinear/problem.rb', line 11

def initialize(labels, examples, bias = -1)
  unless labels.size == examples.size
    raise ArgumentError, 'labels and examples must be same size'
  end
  @prob = Liblinearswig::Problem.new
  @c_label = new_double_array(labels)
  @examples = examples
  @bias = bias
  @max_example_index = max_index(@examples)
  @example_matrix = feature_node_matrix(examples.size)
  @c_example_array = []

  set_example_matrix

  @prob.tap do |p|
    p.y = @c_label
    p.x = @example_matrix
    p.bias = bias
    p.l = examples.size
    p.n = @max_example_index
    p.n += 1 if bias >= 0
  end
end

Instance Attribute Details

#probObject

Returns the value of attribute prob.



5
6
7
# File 'lib/liblinear/problem.rb', line 5

def prob
  @prob
end

Instance Method Details

#set_example_matrixObject



35
36
37
38
39
40
41
# File 'lib/liblinear/problem.rb', line 35

def set_example_matrix
  @examples.size.times do |index|
    c_example = convert_to_feature_node_array(@examples[index], @max_example_index, @bias)
    @c_example_array << c_example
    feature_node_matrix_set(@example_matrix, index, c_example)
  end
end