Sha256: e333d2333727d7a4940e8d46896f2ff22111a698572528129b1dc26b1b290dbc

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eps-0.3.0 lib/eps/metrics.rb