Class: Rumale::EvaluationMeasure::ExplainedVarianceScore

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

Overview

ExplainedVarianceScore is a class that calculates the explained variance score.

Examples:

require 'rumale/evaluation_measure/explained_variance_score'

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

Instance Method Summary collapse

Instance Method Details

#score(y_true, y_pred) ⇒ Float

Calculate explained variance score.

Parameters:

  • y_true (Numo::DFloat)

    (shape: [n_samples, n_outputs]) Ground truth target values.

  • y_pred (Numo::DFloat)

    (shape: [n_samples, n_outputs]) Estimated target values.

Returns:

  • (Float)

    Explained variance score.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/explained_variance_score.rb', line 22

def score(y_true, y_pred)
  diff = y_true - y_pred
  numerator = ((diff - diff.mean(0))**2).mean(0)
  denominator = ((y_true - y_true.mean(0))**2).mean(0)

  n_outputs = y_true.shape[1]
  if n_outputs.nil?
    denominator.zero? ? 0 : 1.0 - numerator / denominator
  else
    valids = denominator.ne(0)
    (1.0 - numerator[valids] / denominator[valids]).sum / n_outputs
  end
end