Sha256: a93a6c666bcc856a3fde251d29c3c8492be66a4584c26491f5f80cc74a6aeec9

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require 'numo/narray'

module Rumale
  module Base
    # Module for all regressors in Rumale.
    module Regressor
      # An abstract method for fitting a model.
      def fit
        raise NotImplementedError, "#{__method__} has to be implemented in #{self.class}."
      end

      # An abstract method for predicting labels.
      def predict
        raise NotImplementedError, "#{__method__} has to be implemented in #{self.class}."
      end

      # Calculate the coefficient of determination for the given testing data.
      #
      # @param x [Numo::DFloat] (shape: [n_samples, n_features]) Testing data.
      # @param y [Numo::DFloat] (shape: [n_samples, n_outputs]) Target values for testing data.
      # @return [Float] Coefficient of determination
      def score(x, y)
        x = ::Rumale::Validation.check_convert_sample_array(x)
        y = ::Rumale::Validation.check_convert_target_value_array(y)
        ::Rumale::Validation.check_sample_size(x, y)

        predicted = predict(x)
        n_samples, n_outputs = y.shape
        numerator = ((y - predicted)**2).sum(axis: 0)
        yt_mean = y.sum(axis: 0) / n_samples
        denominator = ((y - yt_mean)**2).sum(axis: 0)
        if n_outputs.nil?
          denominator.zero? ? 0.0 : 1.0 - numerator.fdiv(denominator)
        else
          scores = 1.0 - numerator / denominator
          scores[denominator.eq(0)] = 0.0
          scores.sum.fdiv(scores.size)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rumale-core-0.29.0 lib/rumale/base/regressor.rb
rumale-core-0.28.1 lib/rumale/base/regressor.rb
rumale-core-0.28.0 lib/rumale/base/regressor.rb
rumale-core-0.27.0 lib/rumale/base/regressor.rb
rumale-core-0.26.0 lib/rumale/base/regressor.rb
rumale-core-0.25.0 lib/rumale/base/regressor.rb
rumale-core-0.24.0 lib/rumale/base/regressor.rb