Class: Rumale::SVM::OneClassSVM
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::SVM::OneClassSVM
- Defined in:
- lib/rumale/svm/one_class_svm.rb
Overview
OneClassSVM is a class that provides One-class Support Vector Machine in LIBSVM with Rumale interface.
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#duel_coef ⇒ Numo::DFloat
Return the coefficients of the support vector in decision function.
-
#fit(x) ⇒ OneClassSVM
Fit the model with given training data.
-
#initialize(nu: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0, shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil) ⇒ OneClassSVM
constructor
Create a new estimator with One-class Support Vector Machine.
-
#intercept ⇒ Numo::DFloat
Return the intercepts in decision function.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#n_support ⇒ Integer
Return the number of support vectors.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#predict_proba(x) ⇒ Numo::DFloat
Predict class probability for samples.
-
#support ⇒ Numo::Int32
Return the indices of support vectors.
-
#support_vectors ⇒ Numo::DFloat
Return the support_vectors.
Constructor Details
#initialize(nu: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0, shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil) ⇒ OneClassSVM
Create a new estimator with One-class Support Vector Machine.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rumale/svm/one_class_svm.rb', line 29 def initialize(nu: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0, shrinking: true, probability: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil) super() @params = {} @params[:nu] = nu.to_f @params[:kernel] = kernel @params[:degree] = degree.to_i @params[:gamma] = gamma.to_f @params[:coef0] = coef0.to_f @params[:shrinking] = shrinking @params[:probability] = probability @params[:cache_size] = cache_size.to_f @params[:tol] = tol.to_f @params[:verbose] = verbose @params[:random_seed] = random_seed.nil? ? nil : random_seed.to_i end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
64 65 66 67 68 |
# File 'lib/rumale/svm/one_class_svm.rb', line 64 def decision_function(x) raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained? x = Rumale::Validation.check_convert_sample_array(x) Numo::Libsvm.decision_function(x, libsvm_params, @model) end |
#duel_coef ⇒ Numo::DFloat
Return the coefficients of the support vector in decision function.
129 130 131 |
# File 'lib/rumale/svm/one_class_svm.rb', line 129 def duel_coef @model[:sv_coef] end |
#fit(x) ⇒ OneClassSVM
Fit the model with given training data.
52 53 54 55 56 57 |
# File 'lib/rumale/svm/one_class_svm.rb', line 52 def fit(x, _y = nil) x = Rumale::Validation.check_convert_sample_array(x) dummy = Numo::DFloat.ones(x.shape[0]) @model = Numo::Libsvm.train(x, dummy, libsvm_params) self end |
#intercept ⇒ Numo::DFloat
Return the intercepts in decision function.
135 136 137 |
# File 'lib/rumale/svm/one_class_svm.rb', line 135 def intercept @model[:rho] end |
#marshal_dump ⇒ Hash
Dump marshal data.
96 97 98 99 |
# File 'lib/rumale/svm/one_class_svm.rb', line 96 def marshal_dump { params: @params, model: @model } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
103 104 105 106 107 |
# File 'lib/rumale/svm/one_class_svm.rb', line 103 def marshal_load(obj) @params = obj[:params] @model = obj[:model] nil end |
#n_support ⇒ Integer
Return the number of support vectors.
123 124 125 |
# File 'lib/rumale/svm/one_class_svm.rb', line 123 def n_support @model[:sv_indices].size end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
75 76 77 78 79 |
# File 'lib/rumale/svm/one_class_svm.rb', line 75 def predict(x) raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained? x = Rumale::Validation.check_convert_sample_array(x) Numo::Int32.cast(Numo::Libsvm.predict(x, libsvm_params, @model)) end |
#predict_proba(x) ⇒ Numo::DFloat
Predict class probability for samples. This method works correctly only if the probability parameter is true.
87 88 89 90 91 92 |
# File 'lib/rumale/svm/one_class_svm.rb', line 87 def predict_proba(x) raise "#{self.class.name}##{__method__} expects to be called after training the model with the fit method." unless trained? raise "#{self.class.name}##{__method__} expects to be called after training the probablity parameters." unless trained_probs? x = Rumale::Validation.check_convert_sample_array(x) Numo::Libsvm.predict_proba(x, libsvm_params, @model) end |
#support ⇒ Numo::Int32
Return the indices of support vectors.
111 112 113 |
# File 'lib/rumale/svm/one_class_svm.rb', line 111 def support @model[:sv_indices] end |
#support_vectors ⇒ Numo::DFloat
Return the support_vectors.
117 118 119 |
# File 'lib/rumale/svm/one_class_svm.rb', line 117 def support_vectors @model[:SV] end |