Class: SVMKit::KernelMachine::KernelSVC
- Inherits:
-
Object
- Object
- SVMKit::KernelMachine::KernelSVC
- Includes:
- Base::BaseEstimator, Base::Classifier
- Defined in:
- lib/svmkit/kernel_machine/kernel_svc.rb
Overview
KernelSVC is a class that implements (Nonlinear) Kernel Support Vector Classifier with the Pegasos algorithm.
Reference
-
- Shalev-Shwartz, Y. Singer, N. Srebro, and A. Cotter, "Pegasos: Primal Estimated sub-GrAdient SOlver for SVM," Mathematical Programming, vol. 127 (1), pp. 3--30, 2011.
Instance Attribute Summary collapse
-
#rng ⇒ Random
readonly
Return the random generator for performing random sampling in the Pegasos algorithm.
-
#weight_vec ⇒ NMatrix
readonly
Return the weight vector for Kernel SVC.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ NMatrix
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ KernelSVC
Fit the model with given training data.
-
#new(reg_param: 1.0, max_iter: 1000, random_seed: 1) ⇒ KernelSVC
constructor
Create a new classifier with Kernel Support Vector Machine by the Pegasos algorithm.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#predict(x) ⇒ NMatrix
Predict class labels for samples.
-
#score(x, y) ⇒ Float
Claculate the mean accuracy of the given testing data.
Constructor Details
#new(reg_param: 1.0, max_iter: 1000, random_seed: 1) ⇒ KernelSVC
Create a new classifier with Kernel Support Vector Machine by the Pegasos algorithm.
46 47 48 49 50 51 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 46 def initialize(params = {}) self.params = DEFAULT_PARAMS.merge(Hash[params.map { |k, v| [k.to_sym, v] }]) self.params[:random_seed] ||= srand @weight_vec = nil @rng = Random.new(self.params[:random_seed]) end |
Instance Attribute Details
#rng ⇒ Random (readonly)
Return the random generator for performing random sampling in the Pegasos algorithm.
36 37 38 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 36 def rng @rng end |
#weight_vec ⇒ NMatrix (readonly)
Return the weight vector for Kernel SVC.
32 33 34 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 32 def weight_vec @weight_vec end |
Instance Method Details
#decision_function(x) ⇒ NMatrix
Calculate confidence scores for samples.
87 88 89 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 87 def decision_function(x) @weight_vec.dot(x.transpose) end |
#fit(x, y) ⇒ KernelSVC
Fit the model with given training data.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 59 def fit(x, y) # Generate binary labels negative_label = y.uniq.sort.shift bin_y = y.to_flat_a.map { |l| l != negative_label ? 1 : -1 } # Initialize some variables. n_training_samples = x.shape[0] rand_ids = [] weight_vec = NMatrix.zeros([1, n_training_samples]) # Start optimization. params[:max_iter].times do |t| # random sampling rand_ids = [*0...n_training_samples].shuffle(random: @rng) if rand_ids.empty? target_id = rand_ids.shift # update the weight vector func = (weight_vec * bin_y[target_id]).dot(x.row(target_id).transpose).to_f func *= bin_y[target_id] / (params[:reg_param] * (t + 1)) weight_vec[target_id] += 1.0 if func < 1.0 end # Store the learned model. @weight_vec = weight_vec * NMatrix.new([1, n_training_samples], bin_y) self end |
#marshal_dump ⇒ Hash
Dump marshal data.
114 115 116 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 114 def marshal_dump { params: params, weight_vec: Utils.dump_nmatrix(@weight_vec), rng: @rng } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
120 121 122 123 124 125 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 120 def marshal_load(obj) self.params = obj[:params] @weight_vec = Utils.restore_nmatrix(obj[:weight_vec]) @rng = obj[:rng] nil end |
#predict(x) ⇒ NMatrix
Predict class labels for samples.
96 97 98 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 96 def predict(x) decision_function(x).map { |v| v >= 0 ? 1 : -1 } end |
#score(x, y) ⇒ Float
Claculate the mean accuracy of the given testing data.
106 107 108 109 110 |
# File 'lib/svmkit/kernel_machine/kernel_svc.rb', line 106 def score(x, y) p = predict(x) n_hits = (y.to_flat_a.map.with_index { |l, n| l == p[n] ? 1 : 0 }).inject(:+) n_hits / y.size.to_f end |