Class: Rumale::Clustering::DBSCAN

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

Overview

DBSCAN is a class that implements DBSCAN cluster analysis.

Reference

  • Ester, M., Kriegel, H-P., Sander, J., and Xu, X., “A density-based algorithm for discovering clusters in large spatial databases with noise,” Proc. KDD’ 96, pp. 266–231, 1996.

Examples:

require 'rumale/clustering/dbscan'

analyzer = Rumale::Clustering::DBSCAN.new(eps: 0.5, min_samples: 5)
cluster_labels = analyzer.fit_predict(samples)

Direct Known Subclasses

SNN

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::ClusterAnalyzer

#score

Constructor Details

#initialize(eps: 0.5, min_samples: 5, metric: 'euclidean') ⇒ DBSCAN

Create a new cluster analyzer with DBSCAN method.

Parameters:

  • eps (Float) (defaults to: 0.5)

    The radius of neighborhood.

  • min_samples (Integer) (defaults to: 5)

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

  • 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.



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

def initialize(eps: 0.5, min_samples: 5, metric: 'euclidean')
  super()
  @params = {
    eps: eps,
    min_samples: min_samples,
    metric: (metric == 'precomputed' ? 'precomputed' : 'euclidean')
  }
end

Instance Attribute Details

#core_sample_idsNumo::Int32 (readonly)

Return the core sample indices.

Returns:

  • (Numo::Int32)

    (shape: [n_core_samples])



25
26
27
# File 'rumale-clustering/lib/rumale/clustering/dbscan.rb', line 25

def core_sample_ids
  @core_sample_ids
end

#labelsNumo::Int32 (readonly)

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

Returns:

  • (Numo::Int32)

    (shape: [n_samples])



29
30
31
# File 'rumale-clustering/lib/rumale/clustering/dbscan.rb', line 29

def labels
  @labels
end

Instance Method Details

#fit(x) ⇒ DBSCAN

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:

  • (DBSCAN)

    The learned cluster analyzer itself.

Raises:

  • (ArgumentError)


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

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)

  partial_fit(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)


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

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)

  partial_fit(x)
  labels
end