Class: Rumale::EvaluationMeasure::LogLoss
- Inherits:
-
Object
- Object
- Rumale::EvaluationMeasure::LogLoss
- Includes:
- Base::Evaluator
- Defined in:
- rumale-evaluation_measure/lib/rumale/evaluation_measure/log_loss.rb
Overview
LogLoss is a class that calculates the logarithmic loss of predicted class probability.
Instance Method Summary collapse
-
#score(y_true, y_pred, eps = 1e-15) ⇒ Float
Calculate mean logarithmic loss.
Instance Method Details
#score(y_true, y_pred, eps = 1e-15) ⇒ Float
Calculate mean logarithmic loss. If both y_true and y_pred are array (both shapes are [n_samples]), this method calculates mean logarithmic loss for binary classification.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/log_loss.rb', line 25 def score(y_true, y_pred, eps = 1e-15) n_samples, n_classes = y_pred.shape clipped_p = y_pred.clip(eps, 1 - eps) log_loss = if n_classes.nil? negative_label = y_true.to_a.uniq.min bin_y_true = Numo::DFloat.cast(y_true.ne(negative_label)) -(bin_y_true * Numo::NMath.log(clipped_p) + (1 - bin_y_true) * Numo::NMath.log(1 - clipped_p)) else binarized_y_true = binarize(y_true) clipped_p /= clipped_p.sum(axis: 1).(1) -(binarized_y_true * Numo::NMath.log(clipped_p)).sum(axis: 1) end log_loss.sum / n_samples end |