Sha256: 3dff1e4dec9b27ce0757c7cf6ada287e55bec786863d63cf4cf9f7349b305a66

Contents?: true

Size: 1.6 KB

Versions: 17

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require 'svmkit/validation'
require 'svmkit/base/evaluator'
require 'svmkit/evaluation_measure/precision_recall'

module SVMKit
  # This module consists of the classes for model evaluation.
  module EvaluationMeasure
    # FScore is a class that calculates the F1-score of the predicted labels.
    #
    # @example
    #   evaluator = SVMKit::EvaluationMeasure::FScore.new
    #   puts evaluator.score(ground_truth, predicted)
    class FScore
      include Base::Evaluator
      include EvaluationMeasure::PrecisionRecall

      # Return the average type for calculation of F1-score.
      # @return [String] ('binary', 'micro', 'macro')
      attr_reader :average

      # Create a new evaluation measure calculater for F1-score.
      #
      # @param average [String] The average type ('binary', 'micro', 'macro')
      def initialize(average: 'binary')
        SVMKit::Validation.check_params_string(average: average)
        @average = average
      end

      # Calculate average F1-score
      #
      # @param y_true [Numo::Int32] (shape: [n_samples]) Ground truth labels.
      # @param y_pred [Numo::Int32] (shape: [n_samples]) Predicted labels.
      # @return [Float] Average F1-score
      def score(y_true, y_pred)
        SVMKit::Validation.check_label_array(y_true)
        SVMKit::Validation.check_label_array(y_pred)

        case @average
        when 'binary'
          f_score_each_class(y_true, y_pred).last
        when 'micro'
          micro_average_f_score(y_true, y_pred)
        when 'macro'
          macro_average_f_score(y_true, y_pred)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
svmkit-0.7.3 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.7.2 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.7.1 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.7.0 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.6.3 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.6.2 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.6.1 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.6.0 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.5.2 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.5.1 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.5.0 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.4.1 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.4.0 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.3.3 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.3.2 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.3.1 lib/svmkit/evaluation_measure/f_score.rb
svmkit-0.3.0 lib/svmkit/evaluation_measure/f_score.rb