Class: Libsvm::Node
- Inherits:
-
Object
- Object
- Libsvm::Node
- 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
-
#index ⇒ Object
The index identifies the feature for which this node represents a value.
-
#value ⇒ Object
The value of this feature in this instance.
Class Method Summary collapse
-
.[](index, value) ⇒ Libsvm::Node
Create a feature node.
-
.features(*vargs) ⇒ Array<Libsvm::Node>
Create an array of features from collection or variable number of float values.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare feature node for equality.
-
#initialize(index = 0, value = 0.0) ⇒ Node
constructor
Create a new feature node.
- #inspect ⇒ Object
Constructor Details
Instance Attribute Details
#index ⇒ Object
The index identifies the feature for which this node represents a value.
|
# File 'lib/libsvm/node.rb', line 79
|
#value ⇒ Object
The value of this feature in this instance.
|
# File 'lib/libsvm/node.rb', line 82
|
Class Method Details
.[](index, value) ⇒ Libsvm::Node
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.
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.
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 |
#inspect ⇒ Object
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 |