Class: Rumale::Decomposition::SparsePCA
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Decomposition::SparsePCA
- Includes:
- Base::Transformer
- Defined in:
- rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb
Overview
SparsePCA is a class that implements Sparse Principal Component Analysis.
Reference
-
Macky, L., “Deflation Methods for Sparse PCA,” Advances in NIPS’08, pp. 1017–1024, 2008.
-
Hein, M. and Bühler, T., “An Inverse Power Method for Nonlinear Eigenproblems with Applications in 1-Spectral Clustering and Sparse PCA,” Advances in NIPS’10, pp. 847–855, 2010.
Instance Attribute Summary collapse
-
#components ⇒ Numo::DFloat
readonly
Returns the principal components.
-
#mean ⇒ Numo::DFloat
readonly
Returns the mean vector.
-
#rng ⇒ Random
readonly
Return the random generator.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ SparsePCA
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, reg_param: 0.001, max_iter: 1000, tol: 1e-6, random_seed: nil) ⇒ SparsePCA
constructor
Create a new transformer with Sparse PCA.
-
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
Constructor Details
#initialize(n_components: 2, reg_param: 0.001, max_iter: 1000, tol: 1e-6, random_seed: nil) ⇒ SparsePCA
Create a new transformer with Sparse PCA.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 47 def initialize(n_components: 2, reg_param: 0.001, max_iter: 1000, tol: 1e-6, random_seed: nil) super() warn('reg_param should be in the interval [0, 1].') unless (0..1).cover?(reg_param) @params = { n_components: n_components, reg_param: reg_param, max_iter: max_iter, tol: tol, random_seed: random_seed || srand } @rng = Random.new(@params[:random_seed]) end |
Instance Attribute Details
#components ⇒ Numo::DFloat (readonly)
Returns the principal components.
30 31 32 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 30 def components @components end |
#mean ⇒ Numo::DFloat (readonly)
Returns the mean vector.
34 35 36 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 34 def mean @mean end |
#rng ⇒ Random (readonly)
Return the random generator.
38 39 40 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 38 def rng @rng end |
Instance Method Details
#fit(x) ⇒ SparsePCA
Fit the model with given training data.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 67 def fit(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) # initialize some variables. @components = Numo::DFloat.zeros(@params[:n_components], x.shape[1]) # centering. @mean = x.mean(axis: 0) centered_x = x - @mean # optimization. partial_fit(centered_x) @components = @components[0, true].dup if @params[:n_components] == 1 self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
90 91 92 93 94 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 90 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.
100 101 102 103 104 |
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 100 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) (x - @mean).dot(@components.transpose) end |