Class: Libsvm::Node

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

Overview

This class represents a feature.

A feature has an index and a value.

An array of features (Libsvm::Node instances) represents an example which can be classified, or in greater number can constitute the main part of a training set (Libsvm::Prolem).

This class represents the struct svm_node.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new feature node.

Libsvm::Node.new(0, 1.1) # => #<Libsvm::Node: index=0, value=1.1>


88
89
90
91
# File 'lib/libsvm/node.rb', line 88

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

Instance Attribute Details

#indexObject

The index identifies the feature for which this node represents a value.



# File 'lib/libsvm/node.rb', line 79

#valueObject

The value of this feature in this instance.



# File 'lib/libsvm/node.rb', line 82

Class Method Details

.[](index, value) ⇒ Libsvm::Node

Create a feature node.

Libsvm::Node[0, 1.1] # => #<Libsvm::Node: index=0, value=1.1>

Returns:



74
75
76
# File 'lib/libsvm/node.rb', line 74

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

.features(0.3, 0.7, 0.8, ...) ⇒ Array<Libsvm::Node> .features([0.3, 0.7, 0.8, ...]) ⇒ Array<Libsvm::Node> .features([[0, 0.3], [1, 0.7], [2, 0.8], ...]) ⇒ Array<Libsvm::Node> .features(0 = >0.3, 1 = >0.7, 2 = >0.8, 99 = >0.1) ⇒ Array<Libsvm::Node>

Create an array of features from collection or variable number of float values.

Indices are converted to Integer, values are converted to Float. When passed a variable number of Float values or an array of Floast, the index is implied by the arguments position. Collection of tuples of hash arguments afford sparse feature arrays. These can represent features which don’t have consecutive indices starting at zero.

Parameters:

  • vararg

    variable number of value arguments, or

  • array

    an array of values, or

  • array

    an array of [index, value] tuples, or

  • hash

    a hash from index to value

Returns:



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
65
66
67
# File 'lib/libsvm/node.rb', line 39

def features(*vargs)
  array_of_nodes = []
  if vargs.size == 1
    argument = vargs.first
    if argument.class == Array
      case argument.first
      when Array
        argument.each do |pair|
          array_of_nodes << Node.new(pair.first.to_i, pair.last.to_f)
        end
      else
        argument.each_with_index do |value, index|
          array_of_nodes << Node.new(index.to_i, value.to_f)
        end
      end
    elsif argument.class == Hash
      argument.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) ⇒ Boolean

Compare feature node for equality.

Nodes with equal index and value are equal.

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/libsvm/node.rb', line 98

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

#inspectObject



104
105
106
107
108
109
# File 'lib/libsvm/node.rb', line 104

def inspect
  vars = %w(index value).map { |name|
    "#{name}=#{send(name)}"
  }.join(", ")
  "#<#{self.class}: #{vars}>"
end