Class: Rumale::Decomposition::NMF

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Transformer
Defined in:
lib/rumale/decomposition/nmf.rb

Overview

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

Reference

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

Examples:

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

Instance Attribute Summary collapse

Attributes included from Base::BaseEstimator

#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.



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

def initialize(n_components: 2, max_iter: 500, tol: 1.0e-4, eps: 1.0e-16, random_seed: nil)
  check_params_integer(n_components: n_components, max_iter: max_iter)
  check_params_float(tol: tol, eps: eps)
  check_params_type_or_nil(Integer, random_seed: random_seed)
  check_params_positive(n_components: n_components, max_iter: max_iter, tol: tol, eps: eps)
  @params = {}
  @params[:n_components] = n_components
  @params[:max_iter] = max_iter
  @params[:tol] = tol
  @params[:eps] = eps
  @params[:random_seed] = random_seed
  @params[:random_seed] ||= srand
  @components = nil
  @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])



23
24
25
# File 'lib/rumale/decomposition/nmf.rb', line 23

def components
  @components
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


27
28
29
# File 'lib/rumale/decomposition/nmf.rb', line 27

def rng
  @rng
end

Instance Method Details

#fit(x) ⇒ NMF

Fit the model with given training data.

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.



58
59
60
61
62
# File 'lib/rumale/decomposition/nmf.rb', line 58

def fit(x, _y = nil)
  check_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.

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



70
71
72
73
# File 'lib/rumale/decomposition/nmf.rb', line 70

def fit_transform(x, _y = nil)
  check_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.



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

def inverse_transform(z)
  check_sample_array(z)
  z.dot(@components)
end

#marshal_dumpHash

Dump marshal data.

Returns:

  • (Hash)

    The marshal data.



95
96
97
98
99
# File 'lib/rumale/decomposition/nmf.rb', line 95

def marshal_dump
  { params: @params,
    components: @components,
    rng: @rng }
end

#marshal_load(obj) ⇒ nil

Load marshal data.

Returns:

  • (nil)


103
104
105
106
107
108
# File 'lib/rumale/decomposition/nmf.rb', line 103

def marshal_load(obj)
  @params = obj[:params]
  @components = obj[:components]
  @rng = obj[:rng]
  nil
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.



79
80
81
82
# File 'lib/rumale/decomposition/nmf.rb', line 79

def transform(x)
  check_sample_array(x)
  partial_fit(x, false)
end