Class: Rumale::SVM::LinearSVC

Inherits:
Base::Estimator
  • Object
show all
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.

Examples:

estimator = Rumale::SVM::LinearSVC.new(penalty: 'l2', loss: 'squared_hinge', reg_param: 1.0, random_seed: 1)
estimator.fit(training_samples, traininig_labels)
results = estimator.predict(testing_samples)

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • penalty (String) (defaults to: 'l2')

    The type of norm used in the penalization (‘l2’ or ‘l1’).

  • loss (String) (defaults to: 'squared_hinge')

    The type of loss function (‘squared_hinge’ or ‘hinge’). This parameter is ignored if penalty = ‘l1’.

  • dual (Boolean) (defaults to: true)

    The flag indicating whether to solve dual optimization problem. When n_samples > n_features, dual = false is more preferable. This parameter is ignored if loss = ‘hinge’.

  • reg_param (Float) (defaults to: 1.0)

    The regularization parameter.

  • fit_bias (Boolean) (defaults to: true)

    The flag indicating whether to fit the bias term.

  • bias_scale (Float) (defaults to: 1.0)

    The scale of the bias term. This parameter is ignored if fit_bias = false.

  • probability (Boolean) (defaults to: false)

    The flag indicating whether to train the parameter for probability estimation.

  • tol (Float) (defaults to: 1e-3)

    The tolerance of termination criterion.

  • verbose (Boolean) (defaults to: false)

    The flag indicating whether to output learning process message

  • random_seed (Integer/Nil) (defaults to: nil)

    The seed value using to initialize the random generator.



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_termNumo::DFloat (readonly)

Return the bias term (a.k.a. intercept) for LinearSVC.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



26
27
28
# File 'lib/rumale/svm/linear_svc.rb', line 26

def bias_term
  @bias_term
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector for LinearSVC.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to compute the scores.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Confidence score per sample.



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? ? expand_feature(x) : x
  Numo::Liblinear.decision_function(xx, liblinear_params, @model)
end

#fit(x, y) ⇒ LinearSVC

Fit the model with given training data.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::Int32)

    (shape: [n_samples]) The labels to be used for fitting the model.

Returns:

  • (LinearSVC)

    The learned classifier itself.



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? ? expand_feature(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_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about LinearSVC.



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.

Returns:

  • (nil)


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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the labels.

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Predicted class label per sample.



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? ? expand_feature(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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the probailities.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Predicted probability of each class per sample.



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