class Rumale::SVM::LinearSVR

LinearSVR is a class that provides Support Vector Regressor in LIBLINEAR with Rumale interface.

@example

estimator = Rumale::SVM::LinearSVR.new(reg_param: 1.0, random_seed: 1)
estimator.fit(training_samples, traininig_target_values)
results = estimator.predict(testing_samples)

Attributes

bias_term[R]

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

weight_vec[R]

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

Public Class Methods

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

Create a new regressor with Support Vector Regressor.

@param loss [String] The type of loss function ('squared_epsilon_insensitive' or 'epsilon_insensitive'). @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 loss = 'epsilon_insensitive'.

@param reg_param [Float] The regularization parameter. @param epsilon [Float] The epsilon parameter in loss function of espsilon-svr. @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/linear_svr.rb, line 41
def initialize(loss: 'squared_epsilon_insensitive', dual: true, reg_param: 1.0, epsilon: 0.1,
               fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
  check_params_string(loss: loss)
  check_params_numeric(reg_param: reg_param, epsilon: epsilon, 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[:loss] = loss == 'epsilon_insensitive' ? 'epsilon_insensitive' : 'squared_epsilon_insensitive'
  @params[:dual] = dual
  @params[:reg_param] = reg_param.to_f
  @params[:epsilon] = epsilon.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

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::DFloat] (shape: [n_samples]) The target values to be used for fitting the model. @return [LinearSVR] The learned regressor itself.

# File lib/rumale/svm/linear_svr.rb, line 64
def fit(x, y)
  x = check_convert_sample_array(x)
  y = check_convert_tvalue_array(y)
  check_sample_tvalue_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 LinearSVR.

# File lib/rumale/svm/linear_svr.rb, line 87
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/linear_svr.rb, line 96
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 values for samples.

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

# File lib/rumale/svm/linear_svr.rb, line 78
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::Liblinear.predict(xx, liblinear_params, @model)
end

Private Instance Methods

bias_scale() click to toggle source
# File lib/rumale/svm/linear_svr.rb, line 143
def bias_scale
  @params[:bias_scale]
end
expand_feature(x) click to toggle source
# File lib/rumale/svm/linear_svr.rb, line 106
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/linear_svr.rb, line 139
def fit_bias?
  @params[:fit_bias]
end
liblinear_params() click to toggle source
# File lib/rumale/svm/linear_svr.rb, line 121
def liblinear_params
  res = {}
  res[:solver_type] = solver_type
  res[:eps] = @params[:tol]
  res[:C] = @params[:reg_param]
  res[:p] = @params[:epsilon]
  res[:verbose] = @params[:verbose]
  res[:random_seed] = @params[:random_seed]
  res
end
solver_type() click to toggle source
# File lib/rumale/svm/linear_svr.rb, line 132
def solver_type
  return Numo::Liblinear::SolverType::L2R_L1LOSS_SVR_DUAL if @params[:loss] == 'epsilon_insensitive'
  return Numo::Liblinear::SolverType::L2R_L2LOSS_SVR_DUAL if @params[:dual]

  Numo::Liblinear::SolverType::L2R_L2LOSS_SVR
end
trained?() click to toggle source
# File lib/rumale/svm/linear_svr.rb, line 147
def trained?
  !@model.nil?
end
weight_and_bias(base_weight) click to toggle source
# File lib/rumale/svm/linear_svr.rb, line 111
def weight_and_bias(base_weight)
  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
  [weight_mat, bias_vec]
end