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.
| 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 45 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.
| 28 29 30 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 28 def components @components end | 
#mean ⇒ Numo::DFloat (readonly)
Returns the mean vector.
| 32 33 34 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 32 def mean @mean end | 
#rng ⇒ Random (readonly)
Return the random generator.
| 36 37 38 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 36 def rng @rng end | 
Instance Method Details
#fit(x) ⇒ SparsePCA
Fit the model with given training data.
| 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 65 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.
| 88 89 90 91 92 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 88 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.
| 98 99 100 101 102 | # File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 98 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) (x - @mean).dot(@components.transpose) end |