Class: Rumale::Manifold::LaplacianEigenmaps
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Manifold::LaplacianEigenmaps
- Includes:
- Base::Transformer
- Defined in:
- rumale-manifold/lib/rumale/manifold/laplacian_eigenmaps.rb
Overview
LaplacianEigenmaps is a class that implements Laplacian Eigenmaps.
Reference
-
Belkin, M., and Niyogi, P., “Laplacian Eigenmaps and Spectral Techniques for Embedding and Clustering,” Proc. NIPS’01, pp. 585–591, 2001.
Instance Attribute Summary collapse
-
#embedding ⇒ Numo::DFloat
readonly
Return the data in representation space.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ LaplacianEigenmaps
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, gamma: nil, n_neighbors: 10) ⇒ LaplacianEigenmaps
constructor
Create a new transformer with Laplacian Eigenmaps.
-
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
Constructor Details
#initialize(n_components: 2, gamma: nil, n_neighbors: 10) ⇒ LaplacianEigenmaps
Create a new transformer with Laplacian Eigenmaps.
33 34 35 36 37 38 39 40 |
# File 'rumale-manifold/lib/rumale/manifold/laplacian_eigenmaps.rb', line 33 def initialize(n_components: 2, gamma: nil, n_neighbors: 10) super() @params = { n_components: n_components, gamma: gamma, n_neighbors: [1, n_neighbors].max } end |
Instance Attribute Details
#embedding ⇒ Numo::DFloat (readonly)
Return the data in representation space.
26 27 28 |
# File 'rumale-manifold/lib/rumale/manifold/laplacian_eigenmaps.rb', line 26 def @embedding end |
Instance Method Details
#fit(x) ⇒ LaplacianEigenmaps
Fit the model with given training data.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'rumale-manifold/lib/rumale/manifold/laplacian_eigenmaps.rb', line 47 def fit(x, _y = nil) raise 'LaplacianEigenmaps#fit requires Numo::Linalg but that is not loaded' unless enable_linalg?(warning: false) x = Rumale::Validation.check_convert_sample_array(x) distance_mat = Rumale::PairwiseMetric.squared_error(x) neighbor_graph = k_neighbor_graph(distance_mat, @params[:n_neighbors], true) affinity_mat = if @params[:gamma].nil? neighbor_graph else neighbor_graph * Numo::NMath.exp(-@params[:gamma] * distance_mat) end degree_mat = affinity_mat.sum(axis: 1).diag laplacian_mat = degree_mat - affinity_mat _, eig_vecs = Numo::Linalg.eigh(laplacian_mat, degree_mat, vals_range: 1...(1 + @params[:n_components])) @embedding = @params[:n_components] == 1 ? eig_vecs[true, 0].dup : eig_vecs.dup @x_train = x.dup self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
75 76 77 78 79 80 81 |
# File 'rumale-manifold/lib/rumale/manifold/laplacian_eigenmaps.rb', line 75 def fit_transform(x, _y = nil) unless enable_linalg?(warning: false) raise 'LaplacianEigenmaps#fit_transform requires Numo::Linalg but that is not loaded' end fit(x).transform(x) end |
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'rumale-manifold/lib/rumale/manifold/laplacian_eigenmaps.rb', line 87 def transform(x) x = Rumale::Validation.check_convert_sample_array(x) distance_mat = Rumale::PairwiseMetric.squared_error(x, @x_train) neighbor_graph = k_neighbor_graph(distance_mat, @params[:n_neighbors], false) affinity_mat = if @params[:gamma].nil? neighbor_graph else neighbor_graph * Numo::NMath.exp(-@params[:gamma] * distance_mat) end normalizer = Numo::NMath.sqrt(affinity_mat.mean * affinity_mat.mean(axis: 1)) n_train_samples = @x_train.shape[0] weight_mat = 1.fdiv(n_train_samples) * (affinity_mat.transpose / normalizer).transpose weight_mat.dot(@embedding) end |