Class: Rumale::EvaluationMeasure::DaviesBouldinScore

Inherits:
Object
  • Object
show all
Defined in:
rumale-evaluation_measure/lib/rumale/evaluation_measure/davies_bouldin_score.rb

Overview

DaviesBouldinScore is a class that calculates the Davies-Bouldin score.

Reference

  • Davies, D L., and Bouldin, D W., “A Cluster Separation Measure,” IEEE Trans. Pattern Analysis and Machine Intelligence, Vol. PAMI-1, No. 2, pp. 224–227, 1979.

Examples:

require 'rumale/evaluation_measure/davies_bouldin_score'

evaluator = Rumale::EvaluationMeasure::DaviesBouldinScore.new
puts evaluator.score(x, predicted)

Instance Method Summary collapse

Instance Method Details

#score(x, y) ⇒ Float

Calculates the Davies-Bouldin score.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be used for calculating score.

  • y (Numo::Int32)

    (shape: [n_samples]) The predicted labels for each sample.

Returns:

  • (Float)

    The Davies-Bouldin score.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/davies_bouldin_score.rb', line 24

def score(x, y)
  labels = y.to_a.uniq.sort
  n_clusters = labels.size
  n_dimensions = x.shape[1]

  dist_cluster = Numo::DFloat.zeros(n_clusters)
  centroids = Numo::DFloat.zeros(n_clusters, n_dimensions)

  n_clusters.times do |n|
    cls_samples = x[y.eq(labels[n]), true]
    cls_centroid = cls_samples.mean(0)
    centroids[n, true] = cls_centroid
    dist_cluster[n] = Rumale::PairwiseMetric.euclidean_distance(cls_samples, cls_centroid.expand_dims(0)).mean
  end

  dist_centroid = Rumale::PairwiseMetric.euclidean_distance(centroids)
  dist_centroid[dist_centroid.eq(0)] = Float::INFINITY
  dist_mat = (dist_cluster.expand_dims(1) + dist_cluster) / dist_centroid
  dist_mat[dist_mat.diag_indices] = -Float::INFINITY
  dist_mat.max(0).mean
end