class Rumale::SVM::LogisticRegression

LogisticRegression is a class that provides Logistic Regression in LIBLINEAR with Rumale interface

@example

estimator = Rumale::SVM::LogisticRegression.new(penalty: 'l2', dual: false, reg_param: 1.0, random_seed: 1)
estimator.fit(training_samples, traininig_labels)
results = estimator.predict(testing_samples)

Attributes

bias_term[R]

Return the bias term (a.k.a. intercept) for LogisticRegression. @return [Numo::DFloat] (shape: [n_classes])

weight_vec[R]

Return the weight vector for LogisticRegression. @return [Numo::DFloat] (shape: [n_classes, n_features])

Public Class Methods

new(penalty: 'l2', dual: true, reg_param: 1.0, fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil) click to toggle source

Create a new classifier with Logistic Regression.

@param penalty [String] The type of norm used in the penalization ('l2' or 'l1'). @param dual [Boolean] The flag indicating whether to solve dual optimization problem.

When n_samples > n_features, dual = false is more preferable.
This parameter is ignored if penalty = 'l1'.

@param reg_param [Float] The regularization parameter. @param fit_bias [Boolean] The flag indicating whether to fit the bias term. @param bias_scale [Float] The scale of the bias term.

This parameter is ignored if fit_bias = false.

@param tol [Float] The tolerance of termination criterion. @param verbose [Boolean] The flag indicating whether to output learning process message @param random_seed [Integer/Nil] The seed value using to initialize the random generator.

# File lib/rumale/svm/logistic_regression.rb, line 40
def initialize(penalty: 'l2', dual: true, reg_param: 1.0,
               fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
  check_params_string(penalty: penalty)
  check_params_numeric(reg_param: reg_param, bias_scale: bias_scale, tol: tol)
  check_params_boolean(dual: dual, fit_bias: fit_bias, verbose: verbose)
  check_params_numeric_or_nil(random_seed: random_seed)
  @params = {}
  @params[:penalty] = penalty == 'l1' ? 'l1' : 'l2'
  @params[:dual] = dual
  @params[:reg_param] = reg_param.to_f
  @params[:fit_bias] = fit_bias
  @params[:bias_scale] = bias_scale.to_f
  @params[:tol] = tol.to_f
  @params[:verbose] = verbose
  @params[:random_seed] = random_seed.nil? ? nil : random_seed.to_i
end

Public Instance Methods

decision_function(x) click to toggle source

Calculate confidence scores for samples.

@param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores. @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.

# File lib/rumale/svm/logistic_regression.rb, line 76
def decision_function(x)
  raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
  x = check_convert_sample_array(x)
  xx = fit_bias? ? expand_feature(x) : x
  Numo::Liblinear.decision_function(xx, liblinear_params, @model)
end
fit(x, y) click to toggle source

Fit the model with given training data.

@param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model. @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model. @return [LogisticRegression] The learned classifier itself.

# File lib/rumale/svm/logistic_regression.rb, line 62
def fit(x, y)
  x = check_convert_sample_array(x)
  y = check_convert_label_array(y)
  check_sample_label_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])
  self
end
marshal_dump() click to toggle source

Dump marshal data. @return [Hash] The marshal data about LogisticRegression.

# File lib/rumale/svm/logistic_regression.rb, line 108
def marshal_dump
  { params: @params,
    model: @model,
    weight_vec: @weight_vec,
    bias_term: @bias_term }
end
marshal_load(obj) click to toggle source

Load marshal data. @return [nil]

# File lib/rumale/svm/logistic_regression.rb, line 117
def marshal_load(obj)
  @params = obj[:params]
  @model = obj[:model]
  @weight_vec = obj[:weight_vec]
  @bias_term = obj[:bias_term]
  nil
end
predict(x) click to toggle source

Predict class labels for samples.

@param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels. @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.

# File lib/rumale/svm/logistic_regression.rb, line 87
def predict(x)
  raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
  x = 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) click to toggle source

Predict class probability for samples. This method works correctly only if the probability parameter is true.

@param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the probailities. @return [Numo::DFloat] (shape: [n_samples, n_classes]) Predicted probability of each class per sample.

# File lib/rumale/svm/logistic_regression.rb, line 99
def predict_proba(x)
  raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained?
  x = check_convert_sample_array(x)
  xx = fit_bias? ? expand_feature(x) : x
  Numo::Liblinear.predict_proba(xx, liblinear_params, @model)
end

Private Instance Methods

bias_scale() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 176
def bias_scale
  @params[:bias_scale]
end
binary_class?() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 168
def binary_class?
  @model[:nr_class] == 2
end
expand_feature(x) click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 127
def expand_feature(x)
  n_samples = x.shape[0]
  Numo::NArray.hstack([x, Numo::DFloat.ones([n_samples, 1]) * bias_scale])
end
fit_bias?() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 172
def fit_bias?
  @params[:fit_bias]
end
liblinear_params() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 151
def liblinear_params
  res = {}
  res[:solver_type] = solver_type
  res[:eps] = @params[:tol]
  res[:C] = @params[:reg_param]
  res[:verbose] = @params[:verbose]
  res[:random_seed] = @params[:random_seed]
  res
end
n_classes() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 180
def n_classes
  @model[:nr_class]
end
n_features() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 184
def n_features
  @model[:nr_feature]
end
solver_type() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 161
def solver_type
  return Numo::Liblinear::SolverType::L1R_LR if @params[:penalty] == 'l1'
  return Numo::Liblinear::SolverType::L2R_LR_DUAL if @params[:dual]

  Numo::Liblinear::SolverType::L2R_LR
end
trained?() click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 188
def trained?
  !@model.nil?
end
weight_and_bias(base_weight) click to toggle source
# File lib/rumale/svm/logistic_regression.rb, line 132
def weight_and_bias(base_weight)
  if binary_class?
    bias_vec = 0.0
    weight_mat = base_weight.dup
    if fit_bias?
      bias_vec = weight_mat[-1]
      weight_mat = weight_mat[0...-1].dup
    end
  else
    bias_vec = Numo::DFloat.zeros(n_classes)
    weight_mat = base_weight.reshape(n_features, n_classes).transpose.dup
    if fit_bias?
      bias_vec = weight_mat[true, -1].dup
      weight_mat = weight_mat[true, 0...-1].dup
    end
  end
  [weight_mat, bias_vec]
end