Sha256: e5a3e6c684e7caaa1946b2ca304dab3e563b38907e2b89623fd4b1591def1e04

Contents?: true

Size: 956 Bytes

Versions: 2

Compression:

Stored size: 956 Bytes

Contents

# frozen_string_literal: true

require 'svmkit/base/evaluator'

module SVMKit
  # This module consists of the classes for model evaluation.
  module EvaluationMeasure
    # Accuracy is a class that calculates the accuracy of classifier from the predicted labels.
    #
    # @example
    #   evaluator = SVMKit::EvaluationMeasure::Accuracy.new
    #   puts evaluator.score(ground_truth, predicted)
    class Accuracy
      include Base::Evaluator

      # Claculate mean accuracy.
      #
      # @param y_true [Numo::Int32] (shape: [n_samples]) Ground truth labels.
      # @param y_pred [Numo::Int32] (shape: [n_samples]) Predicted labels.
      # @return [Float] Mean accuracy
      def score(y_true, y_pred)
        SVMKit::Validation.check_label_array(y_true)
        SVMKit::Validation.check_label_array(y_pred)

        (y_true.to_a.map.with_index { |label, n| label == y_pred[n] ? 1 : 0 }).inject(:+) / y_true.size.to_f
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
svmkit-0.2.9 lib/svmkit/evaluation_measure/accuracy.rb
svmkit-0.2.8 lib/svmkit/evaluation_measure/accuracy.rb