Class: Liblinear::FeatureNode

Inherits:
Object
  • Object
show all
Defined in:
lib/liblinear/feature_node.rb

Instance Method Summary collapse

Constructor Details

#initialize(example, max_feature_id, bias = -1)) ⇒ FeatureNode

Returns a new instance of FeatureNode.

Parameters:

  • examples (Array <Float> or Hash)
  • max_feature_id (Integer)
  • bias (Float) (defaults to: -1))

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/liblinear/feature_node.rb', line 6

def initialize(example, max_feature_id, bias = -1)
  example = Liblinear::Example.array_to_hash(example) if example.is_a?(::Array)

  example_indexes = []
  example.each_key do |key|
    example_indexes << key
  end
  example_indexes.sort!

  if bias >= 0
    @feature_node = Liblinearswig.feature_node_array(example_indexes.size + 2)
    Liblinearswig.feature_node_array_set(@feature_node, example_indexes.size, max_feature_id + 1, bias)
    Liblinearswig.feature_node_array_set(@feature_node, example_indexes.size + 1, -1, 0)
  else
    @feature_node = Liblinearswig.feature_node_array(example_indexes.size + 1)
    Liblinearswig.feature_node_array_set(@feature_node, example_indexes.size, -1, 0)
  end

  f_index = 0
  example_indexes.each do |e_index|
    Liblinearswig.feature_node_array_set(@feature_node, f_index, e_index, example[e_index])
    f_index += 1
  end
end

Instance Method Details

#deleteObject


36
37
38
# File 'lib/liblinear/feature_node.rb', line 36

def delete
  Liblinearswig.feature_node_array_destroy(@feature_node)
end

#swigLiblinearswig::Feature_node

Returns:

  • (Liblinearswig::Feature_node)

32
33
34
# File 'lib/liblinear/feature_node.rb', line 32

def swig
  @feature_node
end