Class: Libsvm::Model
- Inherits:
-
Object
- Object
- Libsvm::Model
- Defined in:
- lib/jrb-libsvm/model.rb
Class Method Summary collapse
-
.load(filename) ⇒ Object
Load model from given filename.
-
.parse(string) ⇒ Object
Load model from string.
- .train(problem, parameter) ⇒ Object
Instance Method Summary collapse
-
#classes ⇒ Object
Return the number of classes handled by this model.
-
#cost ⇒ Object
Return the value of the cost parameter.
-
#degree ⇒ Object
Return the value of the degree parameter.
-
#gamma ⇒ Object
Return the value of the gamma parameter.
-
#kernel_type ⇒ Object
Return the kernel type for this model.
- #predict(example, &block) ⇒ Object
- #predict_probability(example) ⇒ Object
-
#save(filename) ⇒ Object
Save model to given filename.
-
#serialize ⇒ Object
Serialize model and return a string.
-
#svm_type ⇒ Object
Return the SVM problem type for this model.
- #to_s ⇒ Object
Class Method Details
.load(filename) ⇒ Object
Load model from given filename. Raises IOError on any error.
68 69 70 71 72 |
# File 'lib/jrb-libsvm/model.rb', line 68 def self.load filename Svm.svm_load_model(filename) rescue java.io.IOException raise IOError.new "Error in loading SVM model from file" end |
.parse(string) ⇒ Object
Load model from string.
75 76 77 78 79 80 |
# File 'lib/jrb-libsvm/model.rb', line 75 def self.parse string reader = BufferedReader.new(StringReader.new(string)) Svm.svm_load_model(reader) rescue java.io.IOException raise IOError.new "Error in loading SVM model from string" end |
.train(problem, parameter) ⇒ Object
10 11 12 |
# File 'lib/jrb-libsvm/model.rb', line 10 def train(problem, parameter) return Svm.svm_train(problem, parameter) end |
Instance Method Details
#classes ⇒ Object
Return the number of classes handled by this model.
41 42 43 |
# File 'lib/jrb-libsvm/model.rb', line 41 def classes self.nr_class end |
#cost ⇒ Object
Return the value of the cost parameter
36 37 38 |
# File 'lib/jrb-libsvm/model.rb', line 36 def cost self.param.c end |
#degree ⇒ Object
Return the value of the degree parameter
26 27 28 |
# File 'lib/jrb-libsvm/model.rb', line 26 def degree self.param.degree end |
#gamma ⇒ Object
Return the value of the gamma parameter
31 32 33 |
# File 'lib/jrb-libsvm/model.rb', line 31 def gamma self.param.gamma end |
#kernel_type ⇒ Object
Return the kernel type for this model
21 22 23 |
# File 'lib/jrb-libsvm/model.rb', line 21 def kernel_type self.param.kernel_type end |
#predict(example, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/jrb-libsvm/model.rb', line 82 def predict(example, &block) if block.nil? return Svm.svm_predict(self, example) else prediction, probabilities = predict_probability(example) yield probabilities return prediction end end |
#predict_probability(example) ⇒ Object
92 93 94 95 |
# File 'lib/jrb-libsvm/model.rb', line 92 def predict_probability(example) probabilities = Java::double[self.classes].new return Svm.svm_predict_probability(self, example, probabilities), probabilities end |
#save(filename) ⇒ Object
Save model to given filename. Raises IOError on any error.
47 48 49 50 51 |
# File 'lib/jrb-libsvm/model.rb', line 47 def save filename Svm.svm_save_model(filename, self) rescue e = java.io.IOException raise IOError.new "Error in saving SVM model to file: #{e}" end |
#serialize ⇒ Object
Serialize model and return a string
54 55 56 57 58 59 60 61 |
# File 'lib/jrb-libsvm/model.rb', line 54 def serialize stream = ByteArrayOutputStream.new do_stream = DataOutputStream.new(stream) Svm.svm_save_model(do_stream, self) stream.to_s rescue java.io.IOException raise IOError.new "Error in serializing SVM model" end |
#svm_type ⇒ Object
Return the SVM problem type for this model
16 17 18 |
# File 'lib/jrb-libsvm/model.rb', line 16 def svm_type self.param.svm_type end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/jrb-libsvm/model.rb', line 62 def to_s serialize end |