Class: EvalRuby::Metrics::PrecisionAtK
- Defined in:
- lib/eval_ruby/metrics/precision_at_k.rb
Overview
Computes Precision@K: the fraction of top-k retrieved documents that are relevant.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#call(retrieved:, relevant:, k: nil, **_kwargs) ⇒ Float
Precision score (0.0-1.0).
Methods inherited from Base
Constructor Details
This class inherits a constructor from EvalRuby::Metrics::Base
Instance Method Details
#call(retrieved:, relevant:, k: nil, **_kwargs) ⇒ Float
Returns precision score (0.0-1.0).
15 16 17 18 19 20 21 22 |
# File 'lib/eval_ruby/metrics/precision_at_k.rb', line 15 def call(retrieved:, relevant:, k: nil, **_kwargs) k ||= retrieved.length top_k = retrieved.first(k) return 0.0 if top_k.empty? hits = top_k.count { |doc| relevant.include?(doc) } hits.to_f / top_k.size end |