Class: SVMKit::Decomposition::NMF

Inherits:
Object
  • Object
show all
Includes:
Base::BaseEstimator, Base::Transformer
Defined in:
lib/svmkit/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 = SVMKit::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.



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

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.



24
25
26
# File 'lib/svmkit/decomposition/nmf.rb', line 24

def components
  @components
end

#rngRandom (readonly)

Return the random generator.



28
29
30
# File 'lib/svmkit/decomposition/nmf.rb', line 28

def rng
  @rng
end

Instance Method Details

#fit(x) ⇒ NMF

Fit the model with given training data.



59
60
61
62
63
# File 'lib/svmkit/decomposition/nmf.rb', line 59

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.



71
72
73
74
# File 'lib/svmkit/decomposition/nmf.rb', line 71

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.



89
90
91
92
# File 'lib/svmkit/decomposition/nmf.rb', line 89

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

#marshal_dumpHash

Dump marshal data.



96
97
98
99
100
# File 'lib/svmkit/decomposition/nmf.rb', line 96

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

#marshal_load(obj) ⇒ nil

Load marshal data.



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

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.



80
81
82
83
# File 'lib/svmkit/decomposition/nmf.rb', line 80

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