Class: Rumale::Clustering::PowerIteration

Inherits:
Base::Estimator show all
Includes:
Base::ClusterAnalyzer
Defined in:
rumale-clustering/lib/rumale/clustering/power_iteration.rb

Overview

PowerIteration is a class that implements power iteration clustering.

Reference

  • Lin, F., and Cohen, W W., “Power Iteration Clustering,” Proc. ICML’10, pp. 655–662, 2010.

Examples:

require 'rumale/clustering/power_iteration'

analyzer = Rumale::Clustering::PowerIteration.new(n_clusters: 10, gamma: 8.0, max_iter: 1000)
cluster_labels = analyzer.fit_predict(samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::ClusterAnalyzer

#score

Constructor Details

#initialize(n_clusters: 8, affinity: 'rbf', gamma: nil, init: 'k-means++', max_iter: 1000, tol: 1.0e-8, eps: 1.0e-5, random_seed: nil) ⇒ PowerIteration

Create a new cluster analyzer with power iteration clustering.

Parameters:

  • n_clusters (Integer) (defaults to: 8)

    The number of clusters.

  • affinity (String) (defaults to: 'rbf')

    The representation of affinity matrix (‘rbf’ or ‘precomputed’).

  • gamma (Float) (defaults to: nil)

    The parameter of rbf kernel, if nil it is 1 / n_features. If affinity = ‘precomputed’, this parameter is ignored.

  • init (String) (defaults to: 'k-means++')

    The initialization method for centroids of K-Means clustering (‘random’ or ‘k-means++’).

  • max_iter (Integer) (defaults to: 1000)

    The maximum number of iterations.

  • tol (Float) (defaults to: 1.0e-8)

    The tolerance of termination criterion.

  • eps (Float) (defaults to: 1.0e-5)

    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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'rumale-clustering/lib/rumale/clustering/power_iteration.rb', line 47

def initialize(n_clusters: 8, affinity: 'rbf', gamma: nil, init: 'k-means++',
               max_iter: 1000, tol: 1.0e-8, eps: 1.0e-5, 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,
    eps: eps,
    random_seed: random_seed || srand
  }
end

Instance Attribute Details

#embeddingNumo::DFloat (readonly)

Return the data in embedded space.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples])



26
27
28
# File 'rumale-clustering/lib/rumale/clustering/power_iteration.rb', line 26

def embedding
  @embedding
end

#labelsNumo::Int32 (readonly)

Return the cluster labels.

Returns:

  • (Numo::Int32)

    (shape: [n_samples])



30
31
32
# File 'rumale-clustering/lib/rumale/clustering/power_iteration.rb', line 30

def labels
  @labels
end

#n_iterInteger (readonly)

Return the number of iterations run for optimization

Returns:

  • (Integer)


34
35
36
# File 'rumale-clustering/lib/rumale/clustering/power_iteration.rb', line 34

def n_iter
  @n_iter
end

Instance Method Details

#fit(x) ⇒ PowerIteration

Analysis clusters with given training data.

Returns The learned cluster analyzer itself.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for cluster analysis. If the affinity is ‘precomputed’, x must be a square affinity matrix (shape: [n_samples, n_samples]).

Returns:

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
# File 'rumale-clustering/lib/rumale/clustering/power_iteration.rb', line 68

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)

  fit_predict(x)
  self
end

#fit_predict(x) ⇒ Numo::Int32

Analysis clusters and assign samples to clusters.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for cluster analysis. If the affinity is ‘precomputed’, x must be a square affinity matrix (shape: [n_samples, n_samples]).

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Predicted cluster label per sample.

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
# File 'rumale-clustering/lib/rumale/clustering/power_iteration.rb', line 81

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)

  affinity_mat = @params[:affinity] == 'precomputed' ? x : ::Rumale::PairwiseMetric.rbf_kernel(x, nil, @params[:gamma])
  @embedding, @n_iter = embedded_space(affinity_mat, @params[:max_iter], @params[:tol].fdiv(affinity_mat.shape[0]))
  @labels = line_kmeans_clustering(@embedding)
end