Class: Rumale::EvaluationMeasure::AdjustedRandScore

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

Overview

AdjustedRandScore is a class that calculates the adjusted rand index.

Reference

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

Examples:

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.



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

def score(y_true, y_pred)
  check_label_array(y_true)
  check_label_array(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).map { |v| comb_two(v) }.sum
  sum_comb_b = table.sum(axis: 0).map { |v| comb_two(v) }.sum
  sum_comb = table.flatten.map { |v| comb_two(v) }.sum
  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