Module: Eps::Metrics
- Defined in:
- lib/eps/metrics.rb
Class Method Summary collapse
- .accuracy(y_true, y_pred, weight: nil) ⇒ Object
- .log_loss(y_true, y_pred, eps: 1e-15, weight: nil) ⇒ Object
- .mae(y_true, y_pred, weight: nil) ⇒ Object
- .me(y_true, y_pred, weight: nil) ⇒ Object
- .rmse(y_true, y_pred, weight: nil) ⇒ Object
Class Method Details
.accuracy(y_true, y_pred, weight: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/eps/metrics.rb', line 19 def accuracy(y_true, y_pred, weight: nil) check_size(y_true, y_pred) values = y_true.zip(y_pred).map { |yt, yp| yt == yp ? 1 : 0 } if weight values.each_with_index do |v, i| values[i] *= weight[i] end values.sum / weight.sum.to_f else values.sum / y_true.size.to_f end end |
.log_loss(y_true, y_pred, eps: 1e-15, weight: nil) ⇒ Object
33 34 35 36 37 |
# File 'lib/eps/metrics.rb', line 33 def log_loss(y_true, y_pred, eps: 1e-15, weight: nil) 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) }, weight: weight) end |
.mae(y_true, y_pred, weight: nil) ⇒ Object
9 10 11 12 |
# File 'lib/eps/metrics.rb', line 9 def mae(y_true, y_pred, weight: nil) check_size(y_true, y_pred) mean(errors(y_true, y_pred).map { |v| v.abs }, weight: weight) end |
.me(y_true, y_pred, weight: nil) ⇒ Object
14 15 16 17 |
# File 'lib/eps/metrics.rb', line 14 def me(y_true, y_pred, weight: nil) check_size(y_true, y_pred) mean(errors(y_true, y_pred), weight: weight) end |
.rmse(y_true, y_pred, weight: nil) ⇒ Object
4 5 6 7 |
# File 'lib/eps/metrics.rb', line 4 def rmse(y_true, y_pred, weight: nil) check_size(y_true, y_pred) Math.sqrt(mean(errors(y_true, y_pred).map { |v| v**2 }, weight: weight)) end |