Module: Rumale::Base::Regressor

Overview

Module for all regressors in Rumale.

Instance Method Summary collapse

Instance Method Details

#fitObject

An abstract method for fitting a model.

Raises:

  • (NotImplementedError)


10
11
12
# File 'rumale-core/lib/rumale/base/regressor.rb', line 10

def fit
  raise NotImplementedError, "#{__method__} has to be implemented in #{self.class}."
end

#predictObject

An abstract method for predicting labels.

Raises:

  • (NotImplementedError)


15
16
17
# File 'rumale-core/lib/rumale/base/regressor.rb', line 15

def predict
  raise NotImplementedError, "#{__method__} has to be implemented in #{self.class}."
end

#score(x, y) ⇒ Float

Calculate the coefficient of determination for the given testing data.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) Testing data.

  • y (Numo::DFloat)

    (shape: [n_samples, n_outputs]) Target values for testing data.

Returns:

  • (Float)

    Coefficient of determination



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'rumale-core/lib/rumale/base/regressor.rb', line 24

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