Class: Rumale::SVM::LinearOneClassSVM

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

Overview

LinearOneClassSVM is a class that provides linear One-class Support Vector Machine in LIBLINEAR with Rumale interface.

Examples:

estimator = Rumale::SVM::LinearOneClassSVM.new(nu: 0.05, 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(nu: 0.05, reg_param: 1.0, tol: 1e-3, verbose: false, random_seed: nil) ⇒ LinearOneClassSVM

Create a new estimator with linear One-class Support Vector Machine.

Parameters:

  • nu (Float) (defaults to: 0.05)

    The fraction of data as outliers. The interval of nu is (0, 1].

  • reg_param (Float) (defaults to: 1.0)

    The regularization parameter.

  • 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
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 31

def initialize(nu: 0.05, reg_param: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
  super()
  @params = {}
  @params[:nu] = nu.to_f
  @params[:reg_param] = reg_param.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_termFloat (readonly)

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

Returns:

  • (Float)


22
23
24
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 22

def bias_term
  @bias_term
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector for LinearOneClassSVM.

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



18
19
20
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 18

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.



60
61
62
63
64
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 60

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

#fit(x) ⇒ LinearOneClassSVM

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.

Returns:



47
48
49
50
51
52
53
54
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 47

def fit(x, _y = nil)
  x = Rumale::Validation.check_convert_sample_array(x)
  dummy = Numo::DFloat.ones(x.shape[0])
  @model = Numo::Liblinear.train(x, dummy, liblinear_params)
  @weight_vec = @model[:w].dup
  @bias_term = @model[:rho]
  self
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data about LinearOneClassSVM.



78
79
80
81
82
83
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 78

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)


87
88
89
90
91
92
93
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 87

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::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 label per sample.



70
71
72
73
74
# File 'lib/rumale/svm/linear_one_class_svm.rb', line 70

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)
  Numo::Int32.cast(Numo::Liblinear.predict(x, liblinear_params, @model))
end