Class: Rumale::KernelMachine::KernelRidgeClassifier

Inherits:
Base::Estimator show all
Includes:
Base::Classifier
Defined in:
rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb

Overview

KernelRidgeClassifier is a class that implements classifier based-on kernel ridge regression. It learns a classifier by converting labels to target values { -1, 1 } and performing kernel ridge regression.

Examples:

require 'numo/linalg/autoloader'
require 'rumale/pairwise_metric'
require 'rumale/kernel_machine/kernel_ridge_classifier'

kernel_mat_train = Rumale::PairwiseMetric::rbf_kernel(training_samples)
kridge = Rumale::KernelMachine::KernelRidgeClassifier.new(reg_param: 0.5)
kridge.fit(kernel_mat_train, traininig_values)

kernel_mat_test = Rumale::PairwiseMetric::rbf_kernel(test_samples, training_samples)
results = kridge.predict(kernel_mat_test)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::Classifier

#score

Constructor Details

#initialize(reg_param: 1.0) ⇒ KernelRidgeClassifier

Create a new regressor with kernel ridge classifier.

Parameters:

  • reg_param (Float/Numo::DFloat) (defaults to: 1.0)

    The regularization parameter.



38
39
40
41
42
43
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 38

def initialize(reg_param: 1.0)
  super()
  @params = {
    reg_param: reg_param
  }
end

Instance Attribute Details

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



29
30
31
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 29

def classes
  @classes
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector.

Returns:

  • (Numo::DFloat)

    (shape: [n_training_sample, n_classes])



33
34
35
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 33

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_testing_samples, n_training_samples]) The kernel matrix between testing samples and training samples to predict values.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) The confidence score per sample.



73
74
75
76
77
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 73

def decision_function(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  x.dot(@weight_vec)
end

#fit(x, y) ⇒ KernelRidgeClassifier

Fit the model with given training data.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_training_samples, n_training_samples]) The kernel matrix of the training data to be used for fitting the model.

  • y (Numo::Int32)

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

Returns:

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 51

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)
  raise ArgumentError, 'Expect the kernel matrix of training data to be square.' unless x.shape[0] == x.shape[1]
  raise 'KernelRidgeClassifier#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)

  y_encoded = Numo::DFloat.cast(::Rumale::Utils.binarize_labels(y)) * 2 - 1
  @classes = Numo::NArray[*y.to_a.uniq.sort]

  n_samples = x.shape[0]
  reg_kernel_mat = x + Numo::DFloat.eye(n_samples) * @params[:reg_param]
  @weight_vec = Numo::Linalg.solve(reg_kernel_mat, y_encoded, driver: 'sym')

  self
end

#predict(x) ⇒ Numo::Int32

Predict class labels for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_testing_samples, n_training_samples]) The kernel matrix between testing samples and training samples to predict the labels.

Returns:

  • (Numo::Int32)

    (shape: [n_testing_samples]) Predicted class label per sample.



84
85
86
87
88
89
90
91
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 84

def predict(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  scores = decision_function(x)
  n_samples, n_classes = scores.shape
  label_ids = scores.max_index(axis: 1) - Numo::Int32.new(n_samples).seq * n_classes
  @classes[label_ids].dup
end