precision_at_k#

scikitplot.corpus.precision_at_k(y_true, y_pred, *, k=10, sample_weight=None, relevance_threshold=1, duplicates='raise', zero_division='warn', average='mean')[source]#

Fraction of the first k ranked identifiers that are relevant.

Parameters:
y_truesequence or mapping

Relevance judgments per query (see _ranking).

y_predsequence or mapping

Rankings per query, best first.

kint or None, default=10

Cutoff. The denominator is k even when fewer are ranked. None divides by the ranking length instead.

sample_weightarray-like of shape (n_queries,), default=None

Per-query weights of the mean.

relevance_thresholdfloat, default=1

Grade from which a judged identifier is relevant.

duplicates{“raise”, “first”}, default=”raise”

What a repeated identifier in a ranking does.

zero_division{“warn”, 0.0, 1.0, np.nan}, default=”warn”

Value when the metric is undefined (only without a cutoff, for an empty ranking).

average{“mean”, None}, default=”mean”

None returns the per-query values.

Returns:
float or numpy.ndarray
Raises:
TypeError, ValueError

For malformed inputs; the message names the query.

Parameters:
  • y_true (Any)

  • y_pred (Any)

  • k (int | None)

  • sample_weight (Any)

  • relevance_threshold (float)

  • duplicates (str)

  • zero_division (Any)

  • average (str | None)

Return type:

Any

Examples

>>> precision_at_k([{"a", "b"}], [["a", "x", "b", "y"]], k=2)
0.5