Class: Rumale::EvaluationMeasure::LogLoss
- Inherits:
-
Object
- Object
- Rumale::EvaluationMeasure::LogLoss
- Includes:
- Base::Evaluator
- Defined in:
- 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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rumale/evaluation_measure/log_loss.rb', line 24 def score(y_true, y_pred, eps = 1e-15) check_params_type(Numo::Int32, y_true: y_true) check_params_type(Numo::DFloat, y_pred: y_pred) 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 encoder = Rumale::Preprocessing::OneHotEncoder.new encoded_y_true = encoder.fit_transform(y_true) clipped_p /= clipped_p.sum(1).(1) -(encoded_y_true * Numo::NMath.log(clipped_p)).sum(1) end log_loss.sum / n_samples end |