Sha256: c1f77ca01485c9ac76989231347a7219147957e12edc2d595821febf18c4fa04

Contents?: true

Size: 1.25 KB

Versions: 7

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require 'rumale/base/evaluator'

module Rumale
  module EvaluationMeasure
    # Purity is a class that calculates the purity of cluatering results.
    #
    # @example
    #   require 'rumale/evaluation_measure/purity'
    #
    #   evaluator = Rumale::EvaluationMeasure::Purity.new
    #   puts evaluator.score(ground_truth, predicted)
    #
    # *Reference*
    # - Manning, C D., Raghavan, P., and Schutze, H., "Introduction to Information Retrieval," Cambridge University Press., 2008.
    class Purity
      include ::Rumale::Base::Evaluator

      # Calculate purity
      #
      # @param y_true [Numo::Int32] (shape: [n_samples]) Ground truth labels.
      # @param y_pred [Numo::Int32] (shape: [n_samples]) Predicted cluster labels.
      # @return [Float] Purity
      def score(y_true, y_pred)
        # initiazlie some variables.
        purity = 0
        n_samples = y_pred.size
        class_ids = y_true.to_a.uniq
        cluster_ids = y_pred.to_a.uniq
        # calculate purity.
        cluster_ids.each do |k|
          pr_sample_ids = y_pred.eq(k).where.to_a
          purity += class_ids.map { |j| (pr_sample_ids & y_true.eq(j).where.to_a).size }.max
        end
        purity.fdiv(n_samples)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rumale-evaluation_measure-0.29.0 lib/rumale/evaluation_measure/purity.rb
rumale-evaluation_measure-0.28.1 lib/rumale/evaluation_measure/purity.rb
rumale-evaluation_measure-0.28.0 lib/rumale/evaluation_measure/purity.rb
rumale-evaluation_measure-0.27.0 lib/rumale/evaluation_measure/purity.rb
rumale-evaluation_measure-0.26.0 lib/rumale/evaluation_measure/purity.rb
rumale-evaluation_measure-0.25.0 lib/rumale/evaluation_measure/purity.rb
rumale-evaluation_measure-0.24.0 lib/rumale/evaluation_measure/purity.rb