rumale-manifold 0.28.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 146643ddd999165173dcb3388d01240400288e9093d2d599aeff4348486eb8ce
4
- data.tar.gz: bc2c0eca2e8acfa5c07a28dd7b13283ba505c69c066ae94139ac0dc4033933e5
3
+ metadata.gz: 90e4ed5028c5ec00bc02d2cbc982d524881d64d3a83b9e874aaa25d693a42b58
4
+ data.tar.gz: 5dd03085a372ba20432c842dfea839c1490ace268631bf76040ac45de36f5a4d
5
5
  SHA512:
6
- metadata.gz: 308de32a6c4870c308f8da145a5cd4f6e3558d74457ea0aeb8dbc21c06babbdbb52a97e3dbe6a53b1d01cb622aebad79c69b612737023fbef24ef2a83be422f7
7
- data.tar.gz: 6abd9ef1208b3eee3265bc23b4d785e5e70bf34bd88cd05cd1df457dfc02dc7757de63407ee338cb77d818ca2e7d238b50d14b97d9fb76a687511366ebcf7988
6
+ metadata.gz: 8145595c87833e14579640edfaa30eff46b5cdd34a5883d751bee9548fbd92dc070c5f69a6984ba68465912a9a246f47367ae4dff917b97ec70fdd54a0dd5610
7
+ data.tar.gz: 47ba4f7e544adbec19f864e519886357a9a9757eb44d352ab94a2f100d93f43aee671876f920ab7903682cfc3a39a2b412888b607cdd9ae3f25dd482d517ea15
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2022-2023 Atsushi Tatsuma
1
+ Copyright (c) 2022-2024 Atsushi Tatsuma
2
2
  All rights reserved.
3
3
 
4
4
  Redistribution and use in source and binary forms, with or without
@@ -41,9 +41,9 @@ module Rumale
41
41
 
42
42
  # Fit the model with given training data.
43
43
  #
44
- # @overload fit(x) -> LocallyLinearEmbedding
44
+ # @overload fit(x) -> HessianEigenmaps
45
45
  # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
46
- # @return [LocallyLinearEmbedding] The learned transformer itself.
46
+ # @return [HessianEigenmaps] The learned transformer itself.
47
47
  def fit(x, _y = nil) # rubocop:disable Metrics/AbcSize
48
48
  raise 'HessianEigenmaps#fit requires Numo::Linalg but that is not loaded' unless enable_linalg?(warning: false)
49
49
 
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rumale/base/estimator'
4
+ require 'rumale/base/transformer'
5
+ require 'rumale/pairwise_metric'
6
+ require 'rumale/validation'
7
+
8
+ module Rumale
9
+ module Manifold
10
+ # LocalTangentSpaceAlignment is a class that implements Local Tangent Space Alignment.
11
+ #
12
+ # @example
13
+ # require 'numo/linalg/autoloader'
14
+ # require 'rumale/manifold/local_tangent_space_alignment'
15
+ #
16
+ # lem = Rumale::Manifold::LocalTangentSpaceAlignment.new(n_components: 2, n_neighbors: 15)
17
+ # z = lem.fit_transform(x)
18
+ #
19
+ # *Reference*
20
+ # - Zhang, A., and Zha, H., "Principal Manifolds and Nonlinear Diemnsion Reduction via Local Tangent Space Alignment," SIAM Journal on Scientific Computing, vol. 26, iss. 1, pp. 313-338, 2004.
21
+ class LocalTangentSpaceAlignment < Rumale::Base::Estimator
22
+ include Rumale::Base::Transformer
23
+
24
+ # Return the data in representation space.
25
+ # @return [Numo::DFloat] (shape: [n_samples, n_components])
26
+ attr_reader :embedding
27
+
28
+ # Create a new transformer with Local Tangent Space Alignment.
29
+ #
30
+ # @param n_components [Integer] The number of dimensions on representation space.
31
+ # @param n_neighbors [Integer] The number of nearest neighbors for finding k-nearest neighbors
32
+ # @param reg_param [Float] The reguralization parameter for local gram matrix in transform method.
33
+ def initialize(n_components: 2, n_neighbors: 10, reg_param: 1e-3)
34
+ super()
35
+ @params = {
36
+ n_components: n_components,
37
+ n_neighbors: [1, n_neighbors].max,
38
+ reg_param: reg_param
39
+ }
40
+ end
41
+
42
+ # Fit the model with given training data.
43
+ #
44
+ # @overload fit(x) -> LocalTangentSpaceAlignment
45
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
46
+ # @return [LocalTangentSpaceAlignment] The learned transformer itself.
47
+ def fit(x, _y = nil)
48
+ unless enable_linalg?(warning: false)
49
+ raise 'LocalTangentSpaceAlignment#fit requires Numo::Linalg but that is not loaded'
50
+ end
51
+
52
+ x = Rumale::Validation.check_convert_sample_array(x)
53
+
54
+ n_samples = x.shape[0]
55
+ distance_mat = Rumale::PairwiseMetric.squared_error(x)
56
+ neighbor_ids = neighbor_ids(distance_mat, @params[:n_neighbors], true)
57
+
58
+ affinity_mat = Numo::DFloat.zeros(n_samples, n_samples)
59
+ x_tangent = Numo::DFloat.zeros(@params[:n_neighbors], @params[:n_components] + 1)
60
+ x_tangent[true, 0] = 1.fdiv(Math.sqrt(@params[:n_neighbors]))
61
+
62
+ n_samples.times do |n|
63
+ x_local = x[neighbor_ids[n, true], true]
64
+ x_tangent[true, 1...] = right_singular_vectors(x_local, @params[:n_components])
65
+ weight_mat = x_tangent.dot(x_tangent.transpose)
66
+ neighbor_ids[n, true].each_with_index do |m, i|
67
+ affinity_mat[m, neighbor_ids[n, true]] -= weight_mat[i, true]
68
+ affinity_mat[m, m] += 1
69
+ end
70
+ end
71
+
72
+ kernel_mat = 0.5 * (affinity_mat.transpose + affinity_mat)
73
+ _, eig_vecs = Numo::Linalg.eigh(kernel_mat, vals_range: 1...(1 + @params[:n_components]))
74
+
75
+ @embedding = @params[:n_components] == 1 ? eig_vecs[true, 0].dup : eig_vecs.dup
76
+ @x_train = x.dup
77
+
78
+ self
79
+ end
80
+
81
+ # Fit the model with training data, and then transform them with the learned model.
82
+ #
83
+ # @overload fit_transform(x) -> Numo::DFloat
84
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
85
+ # @return [Numo::DFloat] (shape: [n_samples, n_components]) The transformed data
86
+ def fit_transform(x, _y = nil)
87
+ unless enable_linalg?(warning: false)
88
+ raise 'LocalTangentSpaceAlignment#fit_transform requires Numo::Linalg but that is not loaded'
89
+ end
90
+
91
+ fit(x).transform(x)
92
+ end
93
+
94
+ # Transform the given data with the learned model.
95
+ # For out-of-sample data embedding, the same method as Locally Linear Embedding is used.
96
+ #
97
+ # @param x [Numo::DFloat] (shape: [n_samples, n_features]) The data to be transformed with the learned model.
98
+ # @return [Numo::DFloat] (shape: [n_samples, n_components]) The transformed data.
99
+ def transform(x)
100
+ x = Rumale::Validation.check_convert_sample_array(x)
101
+
102
+ n_samples = x.shape[0]
103
+ tol = @params[:reg_param].fdiv(@params[:n_neighbors])
104
+ distance_mat = Rumale::PairwiseMetric.squared_error(x, @x_train)
105
+ neighbor_ids = neighbor_ids(distance_mat, @params[:n_neighbors], false)
106
+ weight_mat = Numo::DFloat.zeros(n_samples, @x_train.shape[0])
107
+
108
+ n_samples.times do |n|
109
+ x_local = @x_train[neighbor_ids[n, true], true] - x[n, true]
110
+ gram_mat = x_local.dot(x_local.transpose)
111
+ gram_mat += tol * weight_mat.trace * Numo::DFloat.eye(@params[:n_neighbors])
112
+ weights = Numo::Linalg.solve(gram_mat, Numo::DFloat.ones(@params[:n_neighbors]))
113
+ weights /= weights.sum + 1e-8
114
+ weight_mat[n, neighbor_ids[n, true]] = weights
115
+ end
116
+
117
+ weight_mat.dot(@embedding)
118
+ end
119
+
120
+ private
121
+
122
+ def neighbor_ids(distance_mat, n_neighbors, contain_self)
123
+ n_samples = distance_mat.shape[0]
124
+ neighbor_ids = Numo::Int32.zeros(n_samples, n_neighbors)
125
+ if contain_self
126
+ n_samples.times { |n| neighbor_ids[n, true] = (distance_mat[n, true].sort_index.to_a - [n])[0...n_neighbors] }
127
+ else
128
+ n_samples.times { |n| neighbor_ids[n, true] = distance_mat[n, true].sort_index.to_a[0...n_neighbors] }
129
+ end
130
+ neighbor_ids
131
+ end
132
+
133
+ def right_singular_vectors(x_local, n_singulars)
134
+ n_samples = x_local.shape[0]
135
+ x_local -= x_local.mean(0)
136
+ gram_mat = x_local.dot(x_local.transpose)
137
+ _, evecs = Numo::Linalg.eigh(gram_mat, vals_range: (n_samples - n_singulars)...n_samples)
138
+ evecs.reverse(1).dup
139
+ end
140
+ end
141
+ end
142
+ end
@@ -7,7 +7,7 @@ require 'rumale/validation'
7
7
 
8
8
  module Rumale
9
9
  module Manifold
10
- # LocallyLinearEmbedding is a class that implements Loccaly Linear Embedding.
10
+ # LocallyLinearEmbedding is a class that implements Locally Linear Embedding.
11
11
  #
12
12
  # @example
13
13
  # require 'numo/linalg/autoloader'
@@ -5,6 +5,6 @@ module Rumale
5
5
  # Module for data embedding algorithms.
6
6
  module Manifold
7
7
  # @!visibility private
8
- VERSION = '0.28.1'
8
+ VERSION = '1.0.0'
9
9
  end
10
10
  end
@@ -5,6 +5,7 @@ require 'numo/narray'
5
5
  require_relative 'manifold/laplacian_eigenmaps'
6
6
  require_relative 'manifold/locally_linear_embedding'
7
7
  require_relative 'manifold/hessian_eigenmaps'
8
+ require_relative 'manifold/local_tangent_space_alignment'
8
9
  require_relative 'manifold/mds'
9
10
  require_relative 'manifold/tsne'
10
11
  require_relative 'manifold/version'
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rumale-manifold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.28.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-12-24 00:00:00.000000000 Z
10
+ date: 2025-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: numo-narray
@@ -30,28 +29,28 @@ dependencies:
30
29
  requirements:
31
30
  - - "~>"
32
31
  - !ruby/object:Gem::Version
33
- version: 0.28.1
32
+ version: 1.0.0
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - "~>"
39
38
  - !ruby/object:Gem::Version
40
- version: 0.28.1
39
+ version: 1.0.0
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: rumale-decomposition
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
44
  - - "~>"
46
45
  - !ruby/object:Gem::Version
47
- version: 0.28.1
46
+ version: 1.0.0
48
47
  type: :runtime
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
51
  - - "~>"
53
52
  - !ruby/object:Gem::Version
54
- version: 0.28.1
53
+ version: 1.0.0
55
54
  description: |
56
55
  Rumale::Manifold provides data embedding algorithms,
57
56
  such as Multi-dimensional Scaling, Locally Linear Embedding, Laplacian Eigenmaps, Hessian Eigenmaps,
@@ -68,6 +67,7 @@ files:
68
67
  - lib/rumale/manifold.rb
69
68
  - lib/rumale/manifold/hessian_eigenmaps.rb
70
69
  - lib/rumale/manifold/laplacian_eigenmaps.rb
70
+ - lib/rumale/manifold/local_tangent_space_alignment.rb
71
71
  - lib/rumale/manifold/locally_linear_embedding.rb
72
72
  - lib/rumale/manifold/mds.rb
73
73
  - lib/rumale/manifold/tsne.rb
@@ -80,7 +80,6 @@ metadata:
80
80
  source_code_uri: https://github.com/yoshoku/rumale/-/tree/main/rumale-manifold
81
81
  changelog_uri: https://github.com/yoshoku/rumale/-/blob/main/CHANGELOG.md
82
82
  rubygems_mfa_required: 'true'
83
- post_install_message:
84
83
  rdoc_options: []
85
84
  require_paths:
86
85
  - lib
@@ -95,8 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
94
  - !ruby/object:Gem::Version
96
95
  version: '0'
97
96
  requirements: []
98
- rubygems_version: 3.4.22
99
- signing_key:
97
+ rubygems_version: 3.6.2
100
98
  specification_version: 4
101
99
  summary: Rumale::Manifold provides data embedding algorithms with Rumale interface.
102
100
  test_files: []