Class: Rumale::KernelMachine::KernelPCA
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::KernelMachine::KernelPCA
- 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.
Instance Attribute Summary collapse
-
#alphas ⇒ Numo::DFloat
readonly
Returns the eigenvectors of the centered kernel matrix.
-
#lambdas ⇒ Numo::DFloat
readonly
Returns the eigenvalues of the centered kernel matrix.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ KernelPCA
Fit the model with given training data.
-
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
-
#initialize(n_components: 2) ⇒ KernelPCA
constructor
Create a new transformer with Kernel PCA.
-
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
Constructor Details
#initialize(n_components: 2) ⇒ KernelPCA
Create a new transformer with Kernel PCA.
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
#alphas ⇒ Numo::DFloat (readonly)
Returns the eigenvectors of the centered kernel matrix.
34 35 36 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_pca.rb', line 34 def alphas @alphas end |
#lambdas ⇒ Numo::DFloat (readonly)
Returns the eigenvalues of the centered kernel matrix.
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.
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).(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.
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.
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.(1) - @row_mean + @all_mean transformed = centered_kernel_mat.dot(@transform_mat) @params[:n_components] == 1 ? transformed[true, 0].dup : transformed end |