Class: SvmToolkit::Model
- Inherits:
-
Object
- Object
- SvmToolkit::Model
- Defined in:
- lib/svm_toolkit/model.rb
Overview
Extends the Java Model class with some additional methods.
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.
91 92 93 94 95 96 97 |
# File 'lib/svm_toolkit/model.rb', line 91 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
70 71 72 |
# File 'lib/svm_toolkit/model.rb', line 70 def cost self.param.cost end |
#degree ⇒ Object
Return the value of the degree parameter
60 61 62 |
# File 'lib/svm_toolkit/model.rb', line 60 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
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/svm_toolkit/model.rb', line 12 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
65 66 67 |
# File 'lib/svm_toolkit/model.rb', line 65 def gamma self.param.gamma end |
#kernel_type ⇒ Object
Return the kernel type for this model
55 56 57 |
# File 'lib/svm_toolkit/model.rb', line 55 def kernel_type self.param.kernel_type end |
#number_classes ⇒ Object
Return the number of classes handled by this model.
75 76 77 |
# File 'lib/svm_toolkit/model.rb', line 75 def number_classes self.nr_class end |
#predict(problem, instance_number) ⇒ Object
Predict the class of given instance number in given problem.
102 103 104 |
# File 'lib/svm_toolkit/model.rb', line 102 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.
113 114 115 116 117 118 119 120 121 |
# File 'lib/svm_toolkit/model.rb', line 113 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.
81 82 83 84 85 86 87 |
# File 'lib/svm_toolkit/model.rb', line 81 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.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/svm_toolkit/model.rb', line 38 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
50 51 52 |
# File 'lib/svm_toolkit/model.rb', line 50 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.
28 29 30 31 32 33 34 |
# File 'lib/svm_toolkit/model.rb', line 28 def w_squared if self.w_2.size == 1 self.w_2[0] else self.w_2.to_a end end |