Class: SvmToolkit::Model
- Inherits:
-
Object
- Object
- SvmToolkit::Model
- Defined in:
- lib/svm_toolkit/model.rb
Overview
Holds information for a trained SVM model.
Class Method Summary collapse
-
.load(filename) ⇒ Object
Load model from given filename.
Instance Method Summary collapse
-
#cost ⇒ Object
Return the value of the cost parameter.
-
#degree ⇒ Object
Return the value of the degree parameter.
-
#evaluate_dataset(data, params = {}) ⇒ Object
Evaluate model on given data set (an instance of Problem), returning the number of errors made.
-
#gamma ⇒ Object
Return the value of the gamma parameter.
-
#kernel_type ⇒ Object
Return the kernel type for this model.
-
#number_classes ⇒ Object
Return the number of classes handled by this model.
-
#predict(problem, instance_number) ⇒ Object
Predict the class of given instance number in given problem.
-
#predict_values(problem, instance_number) ⇒ Object
Return the values of given instance number of given problem against each decision boundary.
-
#save(filename) ⇒ Object
Save model to given filename.
-
#support_vector_indices ⇒ Object
Return an array of indices of the training instances used as support vectors.
-
#svm_type ⇒ Object
Return the SVM problem type for this model.
-
#w_squared ⇒ Object
Return the value of w squared for the hyperplane.
Class Method Details
.load(filename) ⇒ Object
Load model from given filename. Raises IOError on any error.
90 91 92 93 94 95 96 |
# File 'lib/svm_toolkit/model.rb', line 90 def self.load filename begin Svm.svm_load_model(filename) rescue java.io.IOException raise IOError.new "Error in loading SVM model from file" end end |
Instance Method Details
#cost ⇒ Object
Return the value of the cost parameter
69 70 71 |
# File 'lib/svm_toolkit/model.rb', line 69 def cost self.param.cost end |
#degree ⇒ Object
Return the value of the degree parameter
59 60 61 |
# File 'lib/svm_toolkit/model.rb', line 59 def degree self.param.degree end |
#evaluate_dataset(data, params = {}) ⇒ Object
Evaluate model on given data set (an instance of Problem), returning the number of errors made. Optional parameters include:
-
:evaluator => Evaluator::OverallAccuracy, the name of the class to use for computing performance
-
:print_results => false, whether to print the result for each instance
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/svm_toolkit/model.rb', line 11 def evaluate_dataset(data, params = {}) evaluator = params.fetch(:evaluator, Evaluator::OverallAccuracy) print_results = params.fetch(:print_results, false) performance = evaluator.new data.l.times do |i| pred = Svm.svm_predict(self, data.x[i]) performance.add_result(data.y[i], pred) if print_results puts "Instance #{i}, Prediction: #{pred}, True label: #{data.y[i]}" end end return performance end |
#gamma ⇒ Object
Return the value of the gamma parameter
64 65 66 |
# File 'lib/svm_toolkit/model.rb', line 64 def gamma self.param.gamma end |
#kernel_type ⇒ Object
Return the kernel type for this model
54 55 56 |
# File 'lib/svm_toolkit/model.rb', line 54 def kernel_type self.param.kernel_type end |
#number_classes ⇒ Object
Return the number of classes handled by this model.
74 75 76 |
# File 'lib/svm_toolkit/model.rb', line 74 def number_classes self.nr_class end |
#predict(problem, instance_number) ⇒ Object
Predict the class of given instance number in given problem.
101 102 103 |
# File 'lib/svm_toolkit/model.rb', line 101 def predict(problem, instance_number) Svm.svm_predict(self, problem.x[instance_number]) end |
#predict_values(problem, instance_number) ⇒ Object
Return the values of given instance number of given problem against each decision boundary. (This is the distance of the instance from each boundary.)
Return value is an array if more than one decision boundary.
111 112 113 114 115 116 117 118 119 |
# File 'lib/svm_toolkit/model.rb', line 111 def predict_values(problem, instance_number) dist = Array.new(number_classes*(number_classes-1)/2, 0).to_java(:double) Svm.svm_predict_values(self, problem.x[instance_number], dist) if dist.size == 1 return dist[0] else return dist.to_a end end |
#save(filename) ⇒ Object
Save model to given filename. Raises IOError on any error.
80 81 82 83 84 85 86 |
# File 'lib/svm_toolkit/model.rb', line 80 def save filename begin Svm.svm_save_model(filename, self) rescue java.io.IOException raise IOError.new "Error in saving SVM model to file" end end |
#support_vector_indices ⇒ Object
Return an array of indices of the training instances used as support vectors.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/svm_toolkit/model.rb', line 37 def support_vector_indices result = [] unless sv_indices.nil? sv_indices.size.times do |i| result << sv_indices[i] end end return result end |
#svm_type ⇒ Object
Return the SVM problem type for this model
49 50 51 |
# File 'lib/svm_toolkit/model.rb', line 49 def svm_type self.param.svm_type end |
#w_squared ⇒ Object
Return the value of w squared for the hyperplane. – returned as an array if there is not just one value.
27 28 29 30 31 32 33 |
# File 'lib/svm_toolkit/model.rb', line 27 def w_squared if self.w_2.size == 1 self.w_2[0] else self.w_2.to_a end end |