Sha256: 84eedd3aca355c6cf484f4e09eb946ef3555fa0a3f7d390d8492089048b6dd62

Contents?: true

Size: 1.81 KB

Versions: 19

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require 'rumale/base/evaluator'

module Rumale
  module EvaluationMeasure
    # MutualInformation is a class that calculates the mutual information.
    #
    # @example
    #   evaluator = Rumale::EvaluationMeasure::MutualInformation.new
    #   puts evaluator.score(ground_truth, predicted)
    #
    # *Reference*
    # - N X. Vinh, J. Epps, and J. Bailey, "Information Theoretic Measures for Clusterings Comparison: Variants, Properties, Normalization and Correction for Chance," J. Machine Learning Research, vol. 11, pp. 2837--1854, 2010.
    class MutualInformation
      include Base::Evaluator

      # Calculate mutual information
      #
      # @param y_true [Numo::Int32] (shape: [n_samples]) Ground truth labels.
      # @param y_pred [Numo::Int32] (shape: [n_samples]) Predicted cluster labels.
      # @return [Float] Mutual information.
      def score(y_true, y_pred)
        y_true = check_convert_label_array(y_true)
        y_pred = check_convert_label_array(y_pred)
        # initiazlie some variables.
        mutual_information = 0.0
        n_samples = y_pred.size
        class_ids = y_true.to_a.uniq
        cluster_ids = y_pred.to_a.uniq
        # calculate mutual information.
        cluster_ids.map do |k|
          pr_sample_ids = y_pred.eq(k).where.to_a
          n_pr_samples = pr_sample_ids.size
          class_ids.map do |j|
            tr_sample_ids = y_true.eq(j).where.to_a
            n_tr_samples = tr_sample_ids.size
            n_intr_samples = (pr_sample_ids & tr_sample_ids).size
            if n_intr_samples.positive?
              mutual_information +=
                n_intr_samples.fdiv(n_samples) * Math.log((n_samples * n_intr_samples).fdiv(n_pr_samples * n_tr_samples))
            end
          end
        end
        mutual_information
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rumale-0.18.5 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.18.4 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.18.3 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.18.2 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.18.1 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.18.0 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.17.3 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.17.2 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.17.1 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.17.0 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.16.1 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.16.0 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.15.0 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.14.5 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.14.4 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.14.3 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.14.2 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.14.1 lib/rumale/evaluation_measure/mutual_information.rb
rumale-0.14.0 lib/rumale/evaluation_measure/mutual_information.rb