Class: Rumale::SVM::SVC
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::SVM::SVC
- Includes:
- Base::Classifier
- Defined in:
- lib/rumale/svm/svc.rb
Overview
SVC is a class that provides Kernel C-Support Vector Classifier 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, y) ⇒ SVC
Fit the model with given training data.
-
#initialize(reg_param: 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) ⇒ SVC
constructor
Create a new classifier with Kernel C-Support Vector Classifier.
-
#intercept ⇒ Numo::DFloat
Return the intercepts in decision function.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#n_support ⇒ Numo::Int32
Return the number of support vectors for each class.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#predict_proba(x) ⇒ Numo::DFloat
Predict class probability for samples.
-
#prob_a ⇒ Numo::DFloat
Return the probability parameter alpha.
-
#prob_b ⇒ Numo::DFloat
Return the probability parameter beta.
-
#support ⇒ Numo::Int32
Return the indices of support vectors.
-
#support_vectors ⇒ Numo::DFloat
Return the support_vectors.
Constructor Details
#initialize(reg_param: 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) ⇒ SVC
Create a new classifier with Kernel C-Support Vector Classifier.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rumale/svm/svc.rb', line 32 def initialize(reg_param: 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[:reg_param] = reg_param.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.
69 70 71 72 73 74 |
# File 'lib/rumale/svm/svc.rb', line 69 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) xx = precomputed_kernel? ? add_index_col(x) : x Numo::Libsvm.decision_function(xx, libsvm_params, @model) end |
#duel_coef ⇒ Numo::DFloat
Return the coefficients of the support vector in decision function.
136 137 138 |
# File 'lib/rumale/svm/svc.rb', line 136 def duel_coef @model[:sv_coef] end |
#fit(x, y) ⇒ SVC
Fit the model with given training data.
55 56 57 58 59 60 61 62 |
# File 'lib/rumale/svm/svc.rb', line 55 def fit(x, y) x = Rumale::Validation.check_convert_sample_array(x) y = Rumale::Validation.check_convert_label_array(y) Rumale::Validation.check_sample_size(x, y) xx = precomputed_kernel? ? add_index_col(x) : x @model = Numo::Libsvm.train(xx, y, libsvm_params) self end |
#intercept ⇒ Numo::DFloat
Return the intercepts in decision function.
142 143 144 |
# File 'lib/rumale/svm/svc.rb', line 142 def intercept @model[:rho] end |
#marshal_dump ⇒ Hash
Dump marshal data.
103 104 105 106 |
# File 'lib/rumale/svm/svc.rb', line 103 def marshal_dump { params: @params, model: @model } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
110 111 112 113 114 |
# File 'lib/rumale/svm/svc.rb', line 110 def marshal_load(obj) @params = obj[:params] @model = obj[:model] nil end |
#n_support ⇒ Numo::Int32
Return the number of support vectors for each class.
130 131 132 |
# File 'lib/rumale/svm/svc.rb', line 130 def n_support @model[:nSV] end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
81 82 83 84 85 86 |
# File 'lib/rumale/svm/svc.rb', line 81 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) xx = precomputed_kernel? ? add_index_col(x) : x Numo::Int32.cast(Numo::Libsvm.predict(xx, 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.
94 95 96 97 98 99 |
# File 'lib/rumale/svm/svc.rb', line 94 def predict_proba(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) xx = precomputed_kernel? ? add_index_col(x) : x Numo::Libsvm.predict_proba(xx, libsvm_params, @model) end |
#prob_a ⇒ Numo::DFloat
Return the probability parameter alpha.
148 149 150 |
# File 'lib/rumale/svm/svc.rb', line 148 def prob_a @model[:probA] end |
#prob_b ⇒ Numo::DFloat
Return the probability parameter beta.
154 155 156 |
# File 'lib/rumale/svm/svc.rb', line 154 def prob_b @model[:probB] end |
#support ⇒ Numo::Int32
Return the indices of support vectors.
118 119 120 |
# File 'lib/rumale/svm/svc.rb', line 118 def support @model[:sv_indices] end |
#support_vectors ⇒ Numo::DFloat
Return the support_vectors.
124 125 126 |
# File 'lib/rumale/svm/svc.rb', line 124 def support_vectors precomputed_kernel? ? del_index_col(@model[:SV]) : @model[:SV] end |