Sha256: 762391b5d7d8f9048286f4b29a0148be2fc834684e516c43a0abc8ceed13ce94

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'rumale/base/evaluator'

module Rumale
  module EvaluationMeasure
    # ExplainedVarianceScore is a class that calculates the explained variance score.
    #
    # @example
    #   require 'rumale/evaluation_measure/explained_variance_score'
    #
    #   evaluator = Rumale::EvaluationMeasure::ExplainedVarianceScore.new
    #   puts evaluator.score(ground_truth, predicted)
    class ExplainedVarianceScore
      include ::Rumale::Base::Evaluator

      # Calculate explained variance score.
      #
      # @param y_true [Numo::DFloat] (shape: [n_samples, n_outputs]) Ground truth target values.
      # @param y_pred [Numo::DFloat] (shape: [n_samples, n_outputs]) Estimated target values.
      # @return [Float] Explained variance score.
      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
    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/explained_variance_score.rb
rumale-evaluation_measure-0.28.1 lib/rumale/evaluation_measure/explained_variance_score.rb
rumale-evaluation_measure-0.28.0 lib/rumale/evaluation_measure/explained_variance_score.rb
rumale-evaluation_measure-0.27.0 lib/rumale/evaluation_measure/explained_variance_score.rb
rumale-evaluation_measure-0.26.0 lib/rumale/evaluation_measure/explained_variance_score.rb
rumale-evaluation_measure-0.25.0 lib/rumale/evaluation_measure/explained_variance_score.rb
rumale-evaluation_measure-0.24.0 lib/rumale/evaluation_measure/explained_variance_score.rb