Class: Rumale::SVM::LogisticRegression

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

Overview

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

Examples:

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(penalty: 'l2', dual: true, reg_param: 1.0, fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil) ⇒ LogisticRegression

Create a new classifier with Logistic Regression.

Parameters:

  • penalty (String) (defaults to: 'l2')

    The type of norm used in the penalization (‘l2’ or ‘l1’).

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

  • reg_param (Float) (defaults to: 1.0)

    The regularization parameter.

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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rumale/svm/logistic_regression.rb', line 39

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)
  super()
  @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

Instance Attribute Details

#bias_termNumo::DFloat (readonly)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_classes])



24
25
26
# File 'lib/rumale/svm/logistic_regression.rb', line 24

def bias_term
  @bias_term
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector for LogisticRegression.

Returns:

  • (Numo::DFloat)

    (shape: [n_classes, n_features])



20
21
22
# File 'lib/rumale/svm/logistic_regression.rb', line 20

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.



72
73
74
75
76
77
# File 'lib/rumale/svm/logistic_regression.rb', line 72

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

#fit(x, y) ⇒ LogisticRegression

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

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

Returns:



58
59
60
61
62
63
64
65
66
# File 'lib/rumale/svm/logistic_regression.rb', line 58

def fit(x, y)
  x = Rumale::Validation.check_convert_sample_array(x)
  y = Rumale::Validation.check_convert_label_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 LogisticRegression.



104
105
106
107
108
109
# File 'lib/rumale/svm/logistic_regression.rb', line 104

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)


113
114
115
116
117
118
119
# File 'lib/rumale/svm/logistic_regression.rb', line 113

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



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

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

#predict_proba(x) ⇒ Numo::DFloat

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

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Predicted probability of each class per sample.



95
96
97
98
99
100
# File 'lib/rumale/svm/logistic_regression.rb', line 95

def predict_proba(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_proba(xx, liblinear_params, @model)
end