Class: Rumale::Decomposition::NMF

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

Overview

NMF is a class that implements Non-negative Matrix Factorization.

Reference

  • Xu, W., Liu, X., and Gong, Y., “Document Clustering Based On Non-negative Matrix Factorization,” Proc. SIGIR’ 03 , pp. 267–273, 2003.

Examples:

require 'rumale/decomposition/nmf'

decomposer = Rumale::Decomposition::NMF.new(n_components: 2)
representaion = decomposer.fit_transform(samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(n_components: 2, max_iter: 500, tol: 1.0e-4, eps: 1.0e-16, random_seed: nil) ⇒ NMF

Create a new transformer with NMF.

Parameters:

  • n_components (Integer) (defaults to: 2)

    The number of components.

  • max_iter (Integer) (defaults to: 500)

    The maximum number of iterations.

  • tol (Float) (defaults to: 1.0e-4)

    The tolerance of termination criterion.

  • eps (Float) (defaults to: 1.0e-16)

    A small value close to zero to avoid zero division error.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator.



38
39
40
41
42
43
44
45
46
47
48
# File 'rumale-decomposition/lib/rumale/decomposition/nmf.rb', line 38

def initialize(n_components: 2, max_iter: 500, tol: 1.0e-4, eps: 1.0e-16, random_seed: nil)
  super()
  @params = {
    n_components: n_components,
    max_iter: max_iter,
    tol: tol,
    eps: eps,
    random_seed: random_seed || srand
  }
  @rng = Random.new(@params[:random_seed])
end

Instance Attribute Details

#componentsNumo::DFloat (readonly)

Returns the factorization matrix.

Returns:

  • (Numo::DFloat)

    (shape: [n_components, n_features])



25
26
27
# File 'rumale-decomposition/lib/rumale/decomposition/nmf.rb', line 25

def components
  @components
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


29
30
31
# File 'rumale-decomposition/lib/rumale/decomposition/nmf.rb', line 29

def rng
  @rng
end

Instance Method Details

#fit(x) ⇒ NMF

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:

  • (NMF)

    The learned transformer itself.



55
56
57
58
59
60
# File 'rumale-decomposition/lib/rumale/decomposition/nmf.rb', line 55

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

  partial_fit(x)
  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



67
68
69
70
71
# File 'rumale-decomposition/lib/rumale/decomposition/nmf.rb', line 67

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

  partial_fit(x)
end

#inverse_transform(z) ⇒ Numo::DFloat

Inverse transform the given transformed data with the learned model.

Parameters:

  • z (Numo::DFloat)

    (shape: [n_samples, n_components]) The data to be restored into original space with the learned model.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_featuress]) The restored data.



87
88
89
90
91
# File 'rumale-decomposition/lib/rumale/decomposition/nmf.rb', line 87

def inverse_transform(z)
  z = ::Rumale::Validation.check_convert_sample_array(z)

  z.dot(@components)
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.



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

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

  partial_fit(x, update_comps: false)
end