Class: Rumale::Preprocessing::KernelCalculator

Inherits:
Base::Estimator show all
Includes:
Base::Transformer
Defined in:
rumale-preprocessing/lib/rumale/preprocessing/kernel_calculator.rb

Overview

KernelCalculator is a class that calculates the kernel matrix with training data.

Examples:

require 'rumale/preprocessing/kernel_calculator'
require 'rumale/kernel_machine/kernel_ridge'
require 'rumale/pipeline/pipeline'

transformer = Rumale::Preprocessing::KernelCalculator.new(kernel: 'rbf', gamma: 0.5)
regressor = Rumale::KernelMachine::KernelRidge.new
pipeline = Rumale::Pipeline::Pipeline.new(
  steps: { trs: transfomer, est: regressor }
)
pipeline.fit(x_train, y_train)
results = pipeline.predict(x_test)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(kernel: 'rbf', gamma: 1, degree: 3, coef: 1) ⇒ KernelCalculator

Create a new transformer that transforms feature vectors into a kernel matrix.

Parameters:

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

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

  • gamma (Float) (defaults to: 1)

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

  • degree (Integer) (defaults to: 3)

    The degree parameter in polynomial kernel function.

  • coef (Float) (defaults to: 1)

    The coefficient in poly/sigmoid kernel function.



37
38
39
40
41
42
43
44
45
# File 'rumale-preprocessing/lib/rumale/preprocessing/kernel_calculator.rb', line 37

def initialize(kernel: 'rbf', gamma: 1, degree: 3, coef: 1)
  super()
  @params = {
    kernel: kernel,
    gamma: gamma,
    degree: degree,
    coef: coef
  }
end

Instance Attribute Details

#componentsNumo::DFloat (readonly)

Returns the training data for calculating kernel matrix.

Returns:

  • (Numo::DFloat)

    (shape: n_components, n_features)



29
30
31
# File 'rumale-preprocessing/lib/rumale/preprocessing/kernel_calculator.rb', line 29

def components
  @components
end

Instance Method Details

#fit(x) ⇒ KernelCalculator

Fit the model with given training data.

Parameters:

  • x (Numo::NArray)

    (shape: [n_samples, n_features]) The training data to be used for calculating kernel matrix.

Returns:



52
53
54
55
56
57
# File 'rumale-preprocessing/lib/rumale/preprocessing/kernel_calculator.rb', line 52

def fit(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  @components = x.dup
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Fit the model with training data, and then transform them with the learned model.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for calculating kernel matrix.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_samples]) The calculated kernel matrix.



64
65
66
67
68
# File 'rumale-preprocessing/lib/rumale/preprocessing/kernel_calculator.rb', line 64

def fit_transform(x, y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  fit(x, y).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Transform the given data with the learned model.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The data to be used for calculating kernel matrix with the training data.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_components]) The calculated kernel matrix.



74
75
76
77
78
# File 'rumale-preprocessing/lib/rumale/preprocessing/kernel_calculator.rb', line 74

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

  kernel_mat(x, @components)
end