module Eps module Metrics class << self def rmse(y_true, y_pred) check_size(y_true, y_pred) Math.sqrt(mean(errors(y_true, y_pred).map { |v| v**2 })) end def mae(y_true, y_pred) check_size(y_true, y_pred) mean(errors(y_true, y_pred).map { |v| v.abs }) end def me(y_true, y_pred) check_size(y_true, y_pred) mean(errors(y_true, y_pred)) end def accuracy(y_true, y_pred) check_size(y_true, y_pred) y_true.zip(y_pred).count { |yt, yp| yt == yp } / y_true.size.to_f end # http://wiki.fast.ai/index.php/Log_Loss def log_loss(y_true, y_pred, eps: 1e-15) check_size(y_true, y_pred) p = y_pred.map { |yp| yp.clamp(eps, 1 - eps) } mean(y_true.zip(p).map { |yt, pi| yt == 1 ? -Math.log(pi) : -Math.log(1 - pi) }) end private def check_size(y_true, y_pred) raise ArgumentError, "Different sizes" if y_true.size != y_pred.size end def mean(arr) arr.sum / arr.size.to_f end def errors(y_true, y_pred) y_true.zip(y_pred).map { |yt, yp| yt - yp } end end end end