Class: Rumale::SVM::LinearSVC
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::SVM::LinearSVC
- Includes:
- Base::Classifier
- Defined in:
- lib/rumale/svm/linear_svc.rb
Overview
LinearSVC is a class that provides Support Vector Classifier in LIBLINEAR with Rumale interface.
Instance Attribute Summary collapse
-
#bias_term ⇒ Numo::DFloat
readonly
Return the bias term (a.k.a. intercept) for LinearSVC.
-
#weight_vec ⇒ Numo::DFloat
readonly
Return the weight vector for LinearSVC.
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ LinearSVC
Fit the model with given training data.
-
#initialize(penalty: 'l2', loss: 'squared_hinge', dual: true, reg_param: 1.0, fit_bias: true, bias_scale: 1.0, probability: false, tol: 1e-3, verbose: false, random_seed: nil) ⇒ LinearSVC
constructor
Create a new classifier with Support Vector Classifier.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#predict_proba(x) ⇒ Numo::DFloat
Predict class probability for samples.
Constructor Details
#initialize(penalty: 'l2', loss: 'squared_hinge', dual: true, reg_param: 1.0, fit_bias: true, bias_scale: 1.0, probability: false, tol: 1e-3, verbose: false, random_seed: nil) ⇒ LinearSVC
Create a new classifier with Support Vector Classifier.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rumale/svm/linear_svc.rb', line 44 def initialize(penalty: 'l2', loss: 'squared_hinge', dual: true, reg_param: 1.0, fit_bias: true, bias_scale: 1.0, probability: false, tol: 1e-3, verbose: false, random_seed: nil) super() @params = {} @params[:penalty] = penalty == 'l1' ? 'l1' : 'l2' @params[:loss] = loss == 'hinge' ? 'hinge' : 'squared_hinge' @params[:dual] = dual @params[:reg_param] = reg_param.to_f @params[:fit_bias] = fit_bias @params[:bias_scale] = bias_scale.to_f @params[:probability] = probability @params[:tol] = tol.to_f @params[:verbose] = verbose @params[:random_seed] = random_seed.nil? ? nil : random_seed.to_i end |
Instance Attribute Details
#bias_term ⇒ Numo::DFloat (readonly)
Return the bias term (a.k.a. intercept) for LinearSVC.
26 27 28 |
# File 'lib/rumale/svm/linear_svc.rb', line 26 def bias_term @bias_term end |
#weight_vec ⇒ Numo::DFloat (readonly)
Return the weight vector for LinearSVC.
22 23 24 |
# File 'lib/rumale/svm/linear_svc.rb', line 22 def weight_vec @weight_vec end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
80 81 82 83 84 85 |
# File 'lib/rumale/svm/linear_svc.rb', line 80 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 = fit_bias? ? (x) : x Numo::Liblinear.decision_function(xx, liblinear_params, @model) end |
#fit(x, y) ⇒ LinearSVC
Fit the model with given training data.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rumale/svm/linear_svc.rb', line 65 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 = fit_bias? ? (x) : x @model = Numo::Liblinear.train(xx, y, liblinear_params) @weight_vec, @bias_term = weight_and_bias(@model[:w]) @prob_param = proba_model(decision_function(x), y) self end |
#marshal_dump ⇒ Hash
Dump marshal data.
119 120 121 122 123 124 125 |
# File 'lib/rumale/svm/linear_svc.rb', line 119 def marshal_dump { params: @params, model: @model, weight_vec: @weight_vec, bias_term: @bias_term, prob_param: @prob_param } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
129 130 131 132 133 134 135 136 |
# File 'lib/rumale/svm/linear_svc.rb', line 129 def marshal_load(obj) @params = obj[:params] @model = obj[:model] @weight_vec = obj[:weight_vec] @bias_term = obj[:bias_term] @prob_param = obj[:prob_param] nil end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
91 92 93 94 95 96 |
# File 'lib/rumale/svm/linear_svc.rb', line 91 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 = fit_bias? ? (x) : x Numo::Int32.cast(Numo::Liblinear.predict(xx, liblinear_params, @model)) end |
#predict_proba(x) ⇒ Numo::DFloat
Predict class probability for samples. This method works correctly only if the probability parameter is true.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rumale/svm/linear_svc.rb', line 103 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) if binary_class? probs = Numo::DFloat.zeros(x.shape[0], 2) probs[true, 0] = 1.0 / (Numo::NMath.exp(@prob_param[0] * decision_function(x) + @prob_param[1]) + 1.0) probs[true, 1] = 1.0 - probs[true, 0] else probs = 1.0 / (Numo::NMath.exp(@prob_param[true, 0] * decision_function(x) + @prob_param[true, 1]) + 1.0) probs = (probs.transpose / probs.sum(axis: 1)).transpose.dup end probs end |