Class: Rumale::EvaluationMeasure::AdjustedRandScore

Inherits:
Object
  • Object
show all
Includes:
Base::Evaluator
Defined in:
rumale-evaluation_measure/lib/rumale/evaluation_measure/adjusted_rand_score.rb

Overview

AdjustedRandScore is a class that calculates the adjusted rand index.

Reference

  • Vinh, N X., Epps, J., and Bailey, J., “Information Theoretic Measures for Clusterings Comparison: Variants, Properties, Normalization and Correction for Chance”, J. Machine Learnig Research, Vol. 11, pp.2837–2854, 2010.

Examples:

require 'rumale/evaluation_measure/adjusted_rand_score'

evaluator = Rumale::EvaluationMeasure::AdjustedRandScore.new
puts evaluator.score(ground_truth, predicted)

Instance Method Summary collapse

Instance Method Details

#score(y_true, y_pred) ⇒ Float

Calculate adjusted rand index.

Parameters:

  • y_true (Numo::Int32)

    (shape: [n_samples]) Ground truth labels.

  • y_pred (Numo::Int32)

    (shape: [n_samples]) Predicted cluster labels.

Returns:

  • (Float)

    Adjusted rand index.



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

def score(y_true, y_pred)
  # initiazlie some variables.
  n_samples = y_pred.size
  n_classes = y_true.to_a.uniq.size
  n_clusters = y_pred.to_a.uniq.size

  # check special cases.
  return 1.0 if special_cases?(n_samples, n_classes, n_clusters)

  # calculate adjusted rand index.
  table = contingency_table(y_true, y_pred)
  sum_comb_a = table.sum(axis: 1).to_a.sum { |v| comb_two(v) }
  sum_comb_b = table.sum(axis: 0).to_a.sum { |v| comb_two(v) }
  sum_comb = table.flatten.to_a.sum { |v| comb_two(v) }
  prod_comb = (sum_comb_a * sum_comb_b).fdiv(comb_two(n_samples))
  mean_comb = (sum_comb_a + sum_comb_b).fdiv(2)
  (sum_comb - prod_comb).fdiv(mean_comb - prod_comb)
end