Class: Rumale::Decomposition::SparsePCA

Inherits:
Base::Estimator show all
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.

Examples:

require 'numo/tiny_linalg'
Numo::Linalg = Numo::TinyLinalg

require 'rumale/decomposition/sparse_pca'

decomposer = Rumale::Decomposition::SparsePCA.new(n_components: 2, reg_param: 0.1)
representaion = decomposer.fit_transform(samples)
sparse_components = decomposer.components

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

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.

Parameters:

  • n_components (Integer) (defaults to: 2)

    The number of principal components.

  • reg_param (Float) (defaults to: 0.001)

    The regularization parameter (interval: [0, 1]).

  • max_iter (Integer) (defaults to: 1000)

    The maximum number of iterations.

  • tol (Float) (defaults to: 1e-6)

    The tolerance of termination criterion.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator.



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

#componentsNumo::DFloat (readonly)

Returns the principal components.

Returns:

  • (Numo::DFloat)

    (shape: [n_components, n_features])



30
31
32
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 30

def components
  @components
end

#meanNumo::DFloat (readonly)

Returns the mean vector.

Returns:

  • (Numo::DFloat)

    (shape: [n_features])



34
35
36
# File 'rumale-decomposition/lib/rumale/decomposition/sparse_pca.rb', line 34

def mean
  @mean
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


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.

Returns The learned transformer itself.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

Returns:

  • (SparsePCA)

    The learned transformer itself.



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.

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

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

Returns:

  • (Numo::DFloat)

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



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The data to be transformed with the learned model.

Returns:

  • (Numo::DFloat)

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



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