Class: Rumale::SVM::NuSVR

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

Overview

NuSVR is a class that provides Kernel Nu-Support Vector Regressor in LIBSVM with Rumale interface.

Examples:

estimator = Rumale::SVM::NuSVR.new(nu: 0.5, kernel: 'rbf', gamma: 10.0, random_seed: 1)
estimator.fit(training_samples, traininig_target_values)
results = estimator.predict(testing_samples)

Instance Method Summary collapse

Constructor Details

#initialize(nu: 0.5, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0, shrinking: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil) ⇒ NuSVR

Create a new regressor with Kernel Nu-Support Vector Regressor.

Parameters:

  • nu (Float) (defaults to: 0.5)

    The regularization parameter. The interval of nu is (0, 1].

  • kernel (String) (defaults to: 'rbf')

    The type of kernel function (‘rbf’, ‘linear’, ‘poly’, ‘sigmoid’, and ‘precomputed’).

  • degree (Integer) (defaults to: 3)

    The degree parameter in polynomial kernel function.

  • gamma (Float) (defaults to: 1.0)

    The gamma parameter in rbf/poly/sigmoid kernel function.

  • coef0 (Float) (defaults to: 0.0)

    The coefficient in poly/sigmoid kernel function.

  • shrinking (Boolean) (defaults to: true)

    The flag indicating whether to use the shrinking heuristics.

  • cache_size (Float) (defaults to: 200.0)

    The cache memory size in MB.

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rumale/svm/nu_svr.rb', line 31

def initialize(nu: 0.5, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0,
               shrinking: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil)
  super()
  @params = {}
  @params[:nu] = nu.to_f
  @params[:kernel] = kernel
  @params[:degree] = degree.to_i
  @params[:gamma] = gamma.to_f
  @params[:coef0] = coef0.to_f
  @params[:shrinking] = shrinking
  @params[:cache_size] = cache_size.to_f
  @params[:tol] = tol.to_f
  @params[:verbose] = verbose
  @params[:random_seed] = random_seed.nil? ? nil : random_seed.to_i
end

Instance Method Details

#duel_coefNumo::DFloat

Return the coefficients of the support vector in decision function.

Returns:

  • (Numo::DFloat)

    (shape: [1, n_support_vectors])



109
110
111
# File 'lib/rumale/svm/nu_svr.rb', line 109

def duel_coef
  @model[:sv_coef]
end

#fit(x, y) ⇒ NuSVR

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. If the kernel is ‘precomputed’, x must be a square distance matrix (shape: [n_samples, n_samples]).

  • y (Numo::DFloat)

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

Returns:

  • (NuSVR)

    The learned regressor itself.



53
54
55
56
57
58
59
60
# File 'lib/rumale/svm/nu_svr.rb', line 53

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 = precomputed_kernel? ? add_index_col(x) : x
  @model = Numo::Libsvm.train(xx, y, libsvm_params)
  self
end

#interceptNumo::DFloat

Return the intercepts in decision function.

Returns:

  • (Numo::DFloat)

    (shape: [1])



115
116
117
# File 'lib/rumale/svm/nu_svr.rb', line 115

def intercept
  @model[:rho]
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about SVR.



76
77
78
79
# File 'lib/rumale/svm/nu_svr.rb', line 76

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

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


83
84
85
86
87
# File 'lib/rumale/svm/nu_svr.rb', line 83

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

#n_supportInteger

Return the number of support vectors.

Returns:

  • (Integer)


103
104
105
# File 'lib/rumale/svm/nu_svr.rb', line 103

def n_support
  support.size
end

#predict(x) ⇒ Numo::DFloat

Predict values for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the labels. If the kernel is ‘precomputed’, the shape of x must be [n_samples, n_training_samples].

Returns:

  • (Numo::DFloat)

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



67
68
69
70
71
72
# File 'lib/rumale/svm/nu_svr.rb', line 67

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 = precomputed_kernel? ? add_index_col(x) : x
  Numo::Libsvm.predict(xx, libsvm_params, @model)
end

#supportNumo::Int32

Return the indices of support vectors.

Returns:

  • (Numo::Int32)

    (shape: [n_support_vectors])



91
92
93
# File 'lib/rumale/svm/nu_svr.rb', line 91

def support
  @model[:sv_indices]
end

#support_vectorsNumo::DFloat

Return the support_vectors.

Returns:

  • (Numo::DFloat)

    (shape: [n_support_vectors, n_features])



97
98
99
# File 'lib/rumale/svm/nu_svr.rb', line 97

def support_vectors
  precomputed_kernel? ? del_index_col(@model[:SV]) : @model[:SV]
end