Class: Rumale::EvaluationMeasure::R2Score
- Inherits:
-
Object
- Object
- Rumale::EvaluationMeasure::R2Score
- Includes:
- Base::Evaluator
- Defined in:
- rumale-evaluation_measure/lib/rumale/evaluation_measure/r2_score.rb
Overview
R2Score is a class that calculates the coefficient of determination for the predicted values.
Instance Method Summary collapse
-
#initialize ⇒ R2Score
constructor
Create a new evaluation measure calculater for coefficient of determination.
-
#score(y_true, y_pred) ⇒ Float
Calculate the coefficient of determination.
Constructor Details
#initialize ⇒ R2Score
Create a new evaluation measure calculater for coefficient of determination.
18 |
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/r2_score.rb', line 18 def initialize; end |
Instance Method Details
#score(y_true, y_pred) ⇒ Float
Calculate the coefficient of determination.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/r2_score.rb', line 25 def score(y_true, y_pred) n_samples, n_outputs = y_true.shape numerator = ((y_true - y_pred)**2).sum(axis: 0) yt_mean = y_true.sum(axis: 0) / n_samples denominator = ((y_true - yt_mean)**2).sum(axis: 0) if n_outputs.nil? denominator.zero? ? 0.0 : 1.0 - numerator / denominator else scores = 1 - numerator / denominator scores[denominator.eq(0)] = 0.0 scores.sum / scores.size end end |