Class: Rumale::KernelMachine::KernelPCA

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

Overview

KernelPCA is a class that implements Kernel Principal Component Analysis.

Reference

  • Scholkopf, B., Smola, A., and Muller, K-R., “Nonlinear Component Analysis as a Kernel Eigenvalue Problem,” Neural Computation, Vol. 10 (5), pp. 1299–1319, 1998.

Examples:

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

kernel_mat_train = Rumale::PairwiseMetric::rbf_kernel(training_samples)
kpca = Rumale::KernelMachine::KernelPCA.new(n_components: 2)
mapped_traininig_samples = kpca.fit_transform(kernel_mat_train)

kernel_mat_test = Rumale::PairwiseMetric::rbf_kernel(test_samples, training_samples)
mapped_test_samples = kpca.transform(kernel_mat_test)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(n_components: 2) ⇒ KernelPCA

Create a new transformer with Kernel PCA.

Parameters:

  • n_components (Integer) (defaults to: 2)

    The number of components.



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

def initialize(n_components: 2)
  super()
  @params = {
    n_components: n_components
  }
end

Instance Attribute Details

#alphasNumo::DFloat (readonly)

Returns the eigenvectors of the centered kernel matrix.

Returns:

  • (Numo::DFloat)

    (shape: [n_training_sampes, n_components])



34
35
36
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_pca.rb', line 34

def alphas
  @alphas
end

#lambdasNumo::DFloat (readonly)

Returns the eigenvalues of the centered kernel matrix.

Returns:

  • (Numo::DFloat)

    (shape: [n_components])



30
31
32
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_pca.rb', line 30

def lambdas
  @lambdas
end

Instance Method Details

#fit(x) ⇒ KernelPCA

Fit the model with given training data. To execute this method, Numo::Linalg must be loaded.

Returns The learned transformer itself.

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.

Returns:

  • (KernelPCA)

    The learned transformer itself.

Raises:

  • (ArgumentError)


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

def fit(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)
  raise ArgumentError, 'Expect the kernel matrix of training data to be square.' unless x.shape[0] == x.shape[1]
  raise 'KernelPCA#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)

  n_samples = x.shape[0]
  @row_mean = x.mean(0)
  @all_mean = @row_mean.sum.fdiv(n_samples)
  centered_kernel_mat = x - x.mean(1).expand_dims(1) - @row_mean + @all_mean
  eig_vals, eig_vecs = Numo::Linalg.eigh(centered_kernel_mat,
                                         vals_range: (n_samples - @params[:n_components])...n_samples)
  @alphas = eig_vecs.reverse(1).dup
  @lambdas = eig_vals.reverse.dup
  @transform_mat = @alphas.dot((1.0 / Numo::NMath.sqrt(@lambdas)).diag)
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Fit the model with training data, and then transform them with the learned model. To execute this method, Numo::Linalg must be loaded.

Returns (shape: [n_samples, n_components]) The transformed data.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_components]) The transformed data



77
78
79
80
81
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_pca.rb', line 77

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

  fit(x).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Transform the given data with the learned model.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_testing_samples, n_training_samples]) The kernel matrix between testing samples and training samples to be transformed.

Returns:

  • (Numo::DFloat)

    (shape: [n_testing_samples, n_components]) The transformed data.



88
89
90
91
92
93
94
95
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_pca.rb', line 88

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

  col_mean = x.sum(axis: 1) / @row_mean.shape[0]
  centered_kernel_mat = x - col_mean.expand_dims(1) - @row_mean + @all_mean
  transformed = centered_kernel_mat.dot(@transform_mat)
  @params[:n_components] == 1 ? transformed[true, 0].dup : transformed
end