Class: Svm::Problem

Inherits:
Object
  • Object
show all
Includes:
CrossValidation
Defined in:
lib/svm/problem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CrossValidation

#cross_validate, #find_best_parameters, #results_for_cross_validation

Constructor Details

#initialize(user_options = {}) ⇒ Problem

Returns a new instance of Problem.



24
25
26
27
# File 'lib/svm/problem.rb', line 24

def initialize(user_options = {})
  @nodes_pointers = []
  @options = Options.new(user_options)
end

Instance Attribute Details

#num_featuresObject (readonly)

Returns the value of attribute num_features.



8
9
10
# File 'lib/svm/problem.rb', line 8

def num_features
  @num_features
end

#num_samplesObject (readonly)

Returns the value of attribute num_samples.



7
8
9
# File 'lib/svm/problem.rb', line 7

def num_samples
  @num_samples
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/svm/problem.rb', line 9

def options
  @options
end

#scalerObject

Returns the value of attribute scaler.



11
12
13
# File 'lib/svm/problem.rb', line 11

def scaler
  @scaler
end

Class Method Details

.load_from_csv(csv_path, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/svm/problem.rb', line 13

def self.load_from_csv(csv_path, options = {})
  data = CSV.read(csv_path).collect do |row|
    row.collect { |field| field.to_f }
  end

  instance = self.new(options)
  instance.data = data

  instance
end

Instance Method Details

#data=(samples, weights = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/svm/problem.rb', line 29

def data=(samples, weights = nil)
  @num_samples    = samples.size
  @num_features   = samples.first.size - 1
  @sample_weights = weights if weights

  if options[:scale]
    self.scaler = Scaler.scale(samples)
    scaler.release_data!
  end

  problem_struct[:l] = num_samples
  problem_struct[:svm_node] = FFI::MemoryPointer.new(FFI::Pointer, num_samples)
  problem_struct[:y] = FFI::MemoryPointer.new(FFI::Type::DOUBLE, num_samples)
  problem_struct[:W] = FFI::MemoryPointer.new(FFI::Type::DOUBLE, num_samples)

  # Allocate memory for the samples
  # There are num_samples each with num_features nodes

  num_samples.times.each do |i|
    sample = samples[i].collect(&:to_f)

    sample_value  = sample.first
    sample_xs     = sample[1..sample.size-1]
    sample_weight = sample_weights[i]

    problem_struct[:y].put_double(FFI::Type::DOUBLE.size * i, sample_value)
    problem_struct[:W].put_double(FFI::Type::DOUBLE.size * i, sample_weight)

    # Allocate memory for the sample
    nodes_ptr = NodeStruct.node_array_from(sample_xs)
    problem_struct[:svm_node].put_pointer(FFI::Pointer.size*i, nodes_ptr)

    # We have to keep a reference to the pointer so it is not gargabe collected
    @nodes_pointers << nodes_ptr
  end
end

#estimate_probabilities=(option) ⇒ Object



128
129
130
131
132
# File 'lib/svm/problem.rb', line 128

def estimate_probabilities=(option)
  value = option ? 1 : 0

  options.parameter_struct[:probability] = value
end

#generate_model(more_options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/svm/problem.rb', line 83

def generate_model(more_options = {})
  set(more_options)

  model_pointer = Svm.svm_train(problem_struct.pointer, options.parameter_struct.pointer)
  model_struct = ModelStruct.new(model_pointer)

  model = Model.new(model_struct)
  model.scaler = scaler

  model
end

#label_weights=(weights) ⇒ Object



111
112
113
114
# File 'lib/svm/problem.rb', line 111

def label_weights=(weights)
  options.label_weights = weights
  check_parameters!
end

#labelsObject



107
108
109
# File 'lib/svm/problem.rb', line 107

def labels
  num_samples.times.collect { |i| value(i) }.uniq
end

#lengthObject



79
80
81
# File 'lib/svm/problem.rb', line 79

def length
  problem_struct[:l]
end

#num_samples_for(label) ⇒ Object



103
104
105
# File 'lib/svm/problem.rb', line 103

def num_samples_for(label)
  num_samples.times.count { |i| value(i) == label }
end

#sample(index) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/svm/problem.rb', line 66

def sample(index)
  sample_ptr = @nodes_pointers[index]

  num_features.times.collect do |j|
    node = NodeStruct.new(sample_ptr + NodeStruct.size * j)
    node[:value]
  end
end

#sample_weightsObject



124
125
126
# File 'lib/svm/problem.rb', line 124

def sample_weights
  @sample_weights ||= Array.new(num_samples, 1.0)
end

#sample_weights=(weights) ⇒ Object



120
121
122
# File 'lib/svm/problem.rb', line 120

def sample_weights=(weights)
  @sample_weights = weights
end

#set(custom_options) ⇒ Object



134
135
136
137
# File 'lib/svm/problem.rb', line 134

def set(custom_options)
  options.add(custom_options)
  check_parameters!
end

#suggested_labels_weightsObject



95
96
97
98
99
100
101
# File 'lib/svm/problem.rb', line 95

def suggested_labels_weights
  labels.inject({}) do |hash, label|
    num = num_samples_for(label).to_f
    hash[label.to_i] = num/num_samples
    hash
  end
end

#value(index) ⇒ Object



75
76
77
# File 'lib/svm/problem.rb', line 75

def value(index)
  problem_struct[:y].get_double(FFI::Type::DOUBLE.size * index)
end

#weight_for(i) ⇒ Object



116
117
118
# File 'lib/svm/problem.rb', line 116

def weight_for(i)
  sample_weights[i] || 1.0
end