Class: Rumale::EvaluationMeasure::FScore

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

Overview

FScore is a class that calculates the F1-score of the predicted labels.

Examples:

require 'rumale/evaluation_measure/f_score'

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(average: 'binary') ⇒ FScore

Create a new evaluation measure calculater for F1-score.

Parameters:

  • average (String) (defaults to: 'binary')

    The average type (‘binary’, ‘micro’, ‘macro’)



27
28
29
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/f_score.rb', line 27

def initialize(average: 'binary')
  @average = average
end

Instance Attribute Details

#averageString (readonly)

Return the average type for calculation of F1-score.

Returns:

  • (String)

    (‘binary’, ‘micro’, ‘macro’)



22
23
24
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/f_score.rb', line 22

def average
  @average
end

Instance Method Details

#score(y_true, y_pred) ⇒ Float

Calculate average F1-score

Parameters:

  • y_true (Numo::Int32)

    (shape: [n_samples]) Ground truth labels.

  • y_pred (Numo::Int32)

    (shape: [n_samples]) Predicted labels.

Returns:

  • (Float)

    Average F1-score



36
37
38
39
40
41
42
43
44
45
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/f_score.rb', line 36

def score(y_true, 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