Class: Svm::Model
- Inherits:
-
Object
- Object
- Svm::Model
- Defined in:
- lib/svm/model.rb
Instance Attribute Summary collapse
-
#model_struct ⇒ Object
readonly
Returns the value of attribute model_struct.
-
#scaler ⇒ Object
Returns the value of attribute scaler.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(model_struct) ⇒ Model
constructor
A new instance of Model.
- #labels ⇒ Object
- #number_of_classes ⇒ Object
- #predict(sample) ⇒ Object
- #predict_probabilities(sample) ⇒ Object
- #save(path) ⇒ Object
Constructor Details
#initialize(model_struct) ⇒ Model
Returns a new instance of Model.
9 10 11 |
# File 'lib/svm/model.rb', line 9 def initialize(model_struct) @model_struct = model_struct end |
Instance Attribute Details
#model_struct ⇒ Object (readonly)
Returns the value of attribute model_struct.
6 7 8 |
# File 'lib/svm/model.rb', line 6 def model_struct @model_struct end |
#scaler ⇒ Object
Returns the value of attribute scaler.
7 8 9 |
# File 'lib/svm/model.rb', line 7 def scaler @scaler end |
Class Method Details
.load(path) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/svm/model.rb', line 18 def self.load(path) model_struct_pointer = Svm.svm_load_model(path) raise ModelSerializationError.new("Unable to load model from file. Error: #{result}") unless model_struct_pointer != FFI::Pointer::NULL model_struct = ModelStruct.new(model_struct_pointer) self.new(model_struct) end |
Instance Method Details
#labels ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/svm/model.rb', line 30 def labels labels_array = FFI::MemoryPointer.new(:int, number_of_classes) Svm.svm_get_labels(model_struct, labels_array) labels_array.read_array_of_int(number_of_classes) end |
#number_of_classes ⇒ Object
26 27 28 |
# File 'lib/svm/model.rb', line 26 def number_of_classes Svm.svm_get_nr_class(model_struct) end |
#predict(sample) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/svm/model.rb', line 38 def predict(sample) scaler.scale(sample) if scaler nodes_ptr = NodeStruct.node_array_from(sample) Svm.svm_predict(model_struct, nodes_ptr) end |
#predict_probabilities(sample) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/svm/model.rb', line 45 def predict_probabilities(sample) unless Svm.svm_check_probability_model(model_struct) == 1 raise ModelError.new("Model doesn't have probability info") end scaler.scale(sample) if scaler nodes_ptr = NodeStruct.node_array_from(sample) prob_array = FFI::MemoryPointer.new(:double, number_of_classes) Svm.svm_predict_probability(model_struct, nodes_ptr, prob_array) probabilities = prob_array.read_array_of_double(number_of_classes) number_of_classes.times.inject({}) do |hash, index| label = labels[index] prob = probabilities[index] hash[label] = prob hash end end |
#save(path) ⇒ Object
13 14 15 16 |
# File 'lib/svm/model.rb', line 13 def save(path) result = Svm.svm_save_model(path, model_struct.pointer) raise ModelSerializationError.new("Unable to save model to file. Error: #{result}") unless result == 0 end |