Class: Libsvm::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/libsvm/node.rb,
ext/rb-libsvm/libsvm.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index = 0, value = 0.0) ⇒ Node

Returns a new instance of Node.



31
32
33
34
# File 'lib/libsvm/node.rb', line 31

def initialize(index=0, value=0.0)
  self.index = index
  self.value = value
end

Class Method Details

.[](index, value) ⇒ Object



26
27
28
# File 'lib/libsvm/node.rb', line 26

def [](index, value)
  new(index, value)
end

.features(*vargs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/libsvm/node.rb', line 4

def features(*vargs)
  array_of_nodes = []
  if vargs.size == 1
    if vargs.first.class == Array
      vargs.first.each_with_index do |value, index|
        array_of_nodes << Node.new(index.to_i, value.to_f)
      end
    elsif vargs.first.class == Hash
      vargs.first.each do |index, value|
        array_of_nodes << Node.new(index.to_i, value.to_f)
      end
    else
      raise(ArgumentError.new("Node features need to be a Hash, Array or Floats"))
    end
  else
    vargs.each_with_index do |value, index|
      array_of_nodes << Node.new(index.to_i, value.to_f)
    end
  end
  array_of_nodes
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
# File 'lib/libsvm/node.rb', line 36

def ==(other)
  index == other.index && value == other.value
end