Class: Rumale::Clustering::SpectralClustering
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Clustering::SpectralClustering
- Includes:
- Base::ClusterAnalyzer
- Defined in:
- rumale-clustering/lib/rumale/clustering/spectral_clustering.rb
Overview
SpectralClustering is a class that implements the normalized spectral clustering.
Reference
-
Ng, A Y., Jordan, M I., and Weiss, Y., “On Spectral Clustering: Analyssi and an algorithm,” Proc. NIPS’01, pp. 849–856, 2001.
-
von Luxburg, U., “A tutorial on spectral clustering,” Statistics and Computing, Vol. 17 (4), pp. 395–416, 2007.
Instance Attribute Summary collapse
-
#embedding ⇒ Numo::DFloat
readonly
Return the data in embedded space.
-
#labels ⇒ Numo::Int32
readonly
Return the cluster labels.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x) ⇒ SpectralClustering
Analysis clusters with given training data.
-
#fit_predict(x) ⇒ Numo::Int32
Analysis clusters and assign samples to clusters.
-
#initialize(n_clusters: 2, affinity: 'rbf', gamma: nil, init: 'k-means++', max_iter: 10, tol: 1.0e-8, random_seed: nil) ⇒ SpectralClustering
constructor
Create a new cluster analyzer with normalized spectral clustering.
Methods included from Base::ClusterAnalyzer
Constructor Details
#initialize(n_clusters: 2, affinity: 'rbf', gamma: nil, init: 'k-means++', max_iter: 10, tol: 1.0e-8, random_seed: nil) ⇒ SpectralClustering
Create a new cluster analyzer with normalized spectral clustering.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'rumale-clustering/lib/rumale/clustering/spectral_clustering.rb', line 46 def initialize(n_clusters: 2, affinity: 'rbf', gamma: nil, init: 'k-means++', max_iter: 10, tol: 1.0e-8, random_seed: nil) super() @params = { n_clusters: n_clusters, affinity: affinity, gamma: gamma, init: (init == 'random' ? 'random' : 'k-means++'), max_iter: max_iter, tol: tol, random_seed: random_seed || srand } end |
Instance Attribute Details
#embedding ⇒ Numo::DFloat (readonly)
Return the data in embedded space.
29 30 31 |
# File 'rumale-clustering/lib/rumale/clustering/spectral_clustering.rb', line 29 def @embedding end |
#labels ⇒ Numo::Int32 (readonly)
Return the cluster labels.
33 34 35 |
# File 'rumale-clustering/lib/rumale/clustering/spectral_clustering.rb', line 33 def labels @labels end |
Instance Method Details
#fit(x) ⇒ SpectralClustering
Analysis clusters with given training data. To execute this method, Numo::Linalg must be loaded.
66 67 68 69 70 71 72 73 74 |
# File 'rumale-clustering/lib/rumale/clustering/spectral_clustering.rb', line 66 def fit(x, _y = nil) x = ::Rumale::Validation.check_convert_sample_array(x) raise ArgumentError, 'the input affinity matrix should be square' if check_invalid_array_shape(x) raise 'SpectralClustering#fit requires Numo::Linalg but that is not loaded' unless enable_linalg?(warning: false) fit_predict(x) self end |
#fit_predict(x) ⇒ Numo::Int32
Analysis clusters and assign samples to clusters. To execute this method, Numo::Linalg must be loaded.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'rumale-clustering/lib/rumale/clustering/spectral_clustering.rb', line 82 def fit_predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) raise ArgumentError, 'the input affinity matrix should be square' if check_invalid_array_shape(x) unless enable_linalg?(warning: false) raise 'SpectralClustering#fit_predict requires Numo::Linalg but that is not loaded' end affinity_mat = @params[:metric] == 'precomputed' ? x : ::Rumale::PairwiseMetric.rbf_kernel(x, nil, @params[:gamma]) @embedding = (affinity_mat, @params[:n_clusters]) = ::Rumale::Utils.normalize(@embedding, 'l2') @labels = kmeans_clustering() end |