metrax.DCGAtK#
- class metrax.DCGAtK(total: Array, count: Array)#
Bases:
AverageComputes Discounted Cumulative Gain at k metric.
DCG tells how good a list of search results or recommendations is, based on the relevance of the items and their positions in the list.
This implementation calculates \(DCG@k\) based on the following formula:
\[DCG@K(y, s) = \sum_{i=1}^{K} \text{gain}(y_i) \times \text{rank_discount}(\text{rank}(s_i))\]where
\(y_i\) is the relevance label from the labels,
\(s_i\) is its score from the prediction,
\(\text{rank}(s_i)\) is the 1-based rank of item \(i\).
\(\text{gain}(y_i) = 2^{y_i} - 1\).
\(\text{rank_discount}(\text{rank}(s_i)) = \frac{1}{\log_2(\text{rank}(s_i) + 1)}\).
We get the final formula:
\[DCG@K(y, s) = \sum_{i=1}^{K} \frac{2^{y_i} - 1}{\log_2(\text{rank}(s_i) + 1)}\]Methods
__init__(total, count)compute()Computes final metrics from intermediate values.
compute_value()Wraps compute() and returns a values.Value.
empty()Returns an empty instance (i.e. .merge(Metric.empty()) is a no-op).
from_fun(fun)Calls cls.from_model_output with the return value from fun.
from_model_output(predictions, labels, ks)Creates a DCGAtK metric instance from model output.
from_output(name)Calls cls.from_model_output with model output named name.
merge(other)Returns Metric that is the accumulation of self and other.
reduce()Reduces the metric along it first axis by calling _reduce_merge().
replace(**updates)Returns a new object replacing the specified fields with new values.
Attributes
totalcount- classmethod from_model_output(predictions: Array, labels: Array, ks: Array) DCGAtK#
Creates a DCGAtK metric instance from model output.
- replace(**updates)#
Returns a new object replacing the specified fields with new values.