metrax.PrecisionAtK#

class metrax.PrecisionAtK(total: Array, count: Array)#

Bases: TopKRankingMetric

Computes P@k (precision at k) metrics.

Precision at k (P@k) is a metric that measures the proportion of relevant items found in the top k recommendations. It answers the question: “Out of the K items recommended, how many are actually relevant?”

Given the top \(K\) recommendations, P@K is calculated as:

\[Precision@K = \frac{\text{Number of relevant items in top K}}{K}\]
__init__(total: Array, count: Array) None#

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 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

total

count

__init__(total: Array, count: Array) None#
replace(**updates)#

Returns a new object replacing the specified fields with new values.