Class: Rumale::Clustering::HDBSCAN

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

Overview

HDBSCAN is a class that implements HDBSCAN cluster analysis.

Reference

  • Campello, R J. G. B., Moulavi, D., Zimek, A., and Sander, J., “Hierarchical Density Estimates for Data Clustering, Visualization, and Outlier Detection,” TKDD, Vol. 10 (1), pp. 5:1–5:51, 2015.

  • Campello, R J. G. B., Moulavi, D., and Sander, J., “Density-Based Clustering Based on Hierarchical Density Estimates,” Proc. PAKDD’13, pp. 160–172, 2013.

  • Lelis, L., and Sander, J., “Semi-Supervised Density-Based Clustering,” Proc. ICDM’09, pp. 842–847, 2009.

Examples:

require 'rumale/clustering/hdbscan'

analyzer = Rumale::Clustering::HDBSCAN.new(min_samples: 5)
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(min_samples: 10, min_cluster_size: nil, metric: 'euclidean') ⇒ HDBSCAN

Create a new cluster analyzer with HDBSCAN algorithm.

Parameters:

  • min_samples (Integer) (defaults to: 10)

    The number of neighbor samples to be used for the criterion whether a point is a core point.

  • min_cluster_size (Integer/Nil) (defaults to: nil)

    The minimum size of cluster. If nil is given, it is set equal to min_samples.

  • metric (String) (defaults to: 'euclidean')

    The metric to calculate the distances. If metric is ‘euclidean’, Euclidean distance is calculated for distance between points. If metric is ‘precomputed’, the fit and fit_transform methods expect to be given a distance matrix.



37
38
39
40
41
42
43
44
# File 'rumale-clustering/lib/rumale/clustering/hdbscan.rb', line 37

def initialize(min_samples: 10, min_cluster_size: nil, metric: 'euclidean')
  super()
  @params = {
    min_samples: min_samples,
    min_cluster_size: min_cluster_size || min_samples,
    metric: (metric == 'precomputed' ? 'precomputed' : 'euclidean')
  }
end

Instance Attribute Details

#labelsNumo::Int32 (readonly)

Return the cluster labels. The negative cluster label indicates that the point is noise.

Returns:

  • (Numo::Int32)

    (shape: [n_samples])



28
29
30
# File 'rumale-clustering/lib/rumale/clustering/hdbscan.rb', line 28

def labels
  @labels
end

Instance Method Details

#fit(x) ⇒ HDBSCAN

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 metric is ‘precomputed’, x must be a square distance matrix (shape: [n_samples, n_samples]).

Returns:

  • (HDBSCAN)

    The learned cluster analyzer itself.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
# File 'rumale-clustering/lib/rumale/clustering/hdbscan.rb', line 52

def fit(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)
  raise ArgumentError, 'the input distance 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 samples to be used for cluster analysis. If the metric is ‘precomputed’, x must be a square distance matrix (shape: [n_samples, n_samples]).

Returns:

  • (Numo::Int32)

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

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# File 'rumale-clustering/lib/rumale/clustering/hdbscan.rb', line 65

def fit_predict(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)
  raise ArgumentError, 'the input distance matrix should be square' if check_invalid_array_shape(x)

  distance_mat = @params[:metric] == 'precomputed' ? x : ::Rumale::PairwiseMetric.euclidean_distance(x)
  @labels = partial_fit(distance_mat)
end