Class: Rumale::SVM::LinearSVR

Inherits:
Base::Estimator
  • Object
show all
Includes:
Base::Regressor
Defined in:
lib/rumale/svm/linear_svr.rb

Overview

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

Examples:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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) ⇒ LinearSVR

Create a new regressor with Support Vector Regressor.

Parameters:

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

    The type of loss function (‘squared_epsilon_insensitive’ or ‘epsilon_insensitive’).

  • 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 = ‘epsilon_insensitive’.

  • reg_param (Float) (defaults to: 1.0)

    The regularization parameter.

  • epsilon (Float) (defaults to: 0.1)

    The epsilon parameter in loss function of espsilon-svr.

  • 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.

  • 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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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)
  super()
  @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

Instance Attribute Details

#bias_termNumo::DFloat (readonly)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



25
26
27
# File 'lib/rumale/svm/linear_svr.rb', line 25

def bias_term
  @bias_term
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector for LinearSVR.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



21
22
23
# File 'lib/rumale/svm/linear_svr.rb', line 21

def weight_vec
  @weight_vec
end

Instance Method Details

#fit(x, y) ⇒ LinearSVR

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::DFloat)

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

Returns:

  • (LinearSVR)

    The learned regressor itself.



61
62
63
64
65
66
67
68
69
# File 'lib/rumale/svm/linear_svr.rb', line 61

def fit(x, y)
  x = Rumale::Validation.check_convert_sample_array(x)
  y = Rumale::Validation.check_convert_target_value_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])
  self
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about LinearSVR.



84
85
86
87
88
89
# File 'lib/rumale/svm/linear_svr.rb', line 84

def marshal_dump
  { params: @params,
    model: @model,
    weight_vec: @weight_vec,
    bias_term: @bias_term }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


93
94
95
96
97
98
99
# File 'lib/rumale/svm/linear_svr.rb', line 93

def marshal_load(obj)
  @params = obj[:params]
  @model = obj[:model]
  @weight_vec = obj[:weight_vec]
  @bias_term = obj[:bias_term]
  nil
end

#predict(x) ⇒ Numo::DFloat

Predict values for samples.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_samples]) Predicted value per sample.



75
76
77
78
79
80
# File 'lib/rumale/svm/linear_svr.rb', line 75

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::Liblinear.predict(xx, liblinear_params, @model)
end