FusionPolicy#

class scikitplot.corpus.FusionPolicy(k=60, weights=(), default_weight=1.0, top_k=None)[source]#

Weighted reciprocal rank fusion with one tie-break and returned contributions.

Parameters:
kint, optional

The RRF constant, at least 1. Default 60.

weightsmapping of str to float, or tuple of (str, float) pairs, optional

Weight per leg name. Finite and non-negative; a weight of 0 removes the leg. Stored as a tuple of pairs sorted by leg name, so dict(weights) reads it back and the policy is hashable.

default_weightfloat, optional

Weight of a leg that weights does not name. Default 1.0.

top_kint or None, optional

Number of items to return, at least 0; None returns every item.

Raises:
TypeError

If k or top_k is not an int (bool included), a weight is not a real number, or a leg name is not a str.

ValueError

If k < 1, top_k < 0, or a weight is negative or not finite.

Parameters:

Examples

>>> FusionPolicy.from_alpha(0.0).fuse([("lexical", ["a"]), ("dense", ["z"])])[0].key
'a'
>>> len(FusionPolicy.from_alpha(0.0).fuse([("lexical", ["a"]), ("dense", ["z"])]))
1
default_weight: float = 1.0#
classmethod from_alpha(alpha, *, k=60, top_k=None, lexical='lexical', dense='dense')[source]#

Return the two-leg policy that alpha is shorthand for.

Parameters:
alphafloat

Dense share in [0, 1]: {lexical: 1 - alpha, dense: alpha}. 0 is the lexical leg alone and 1 the dense leg alone.

kint, optional

The RRF constant. Default 60.

top_kint or None, optional

Number of items to return.

lexical, densestr, optional

The two leg names.

Returns:
FusionPolicy

Every other leg name gets weight 0.

Raises:
TypeError

If alpha is not a real number.

ValueError

If alpha is outside [0, 1] or not finite.

Parameters:
Return type:

FusionPolicy

fuse(legs)[source]#

Fuse ranked legs into one ranking.

Parameters:
legsiterable of (str, iterable of str)

Each leg’s name and its keys, best first.

Returns:
list of FusedItem

Descending exact score, ties by ascending key, at most top_k.

Raises:
TypeError

If a leg is not a (name, keys) pair, a name is not a str, or a key is not a str.

ValueError

If a key is the empty string.

Parameters:

legs (Iterable[tuple[str, Iterable[str]]])

Return type:

list[FusedItem]

k: int = 60#
top_k: int | None = None#
weight(leg)[source]#

Return the weight of the leg named leg.

Parameters:

leg (str)

Return type:

float

weights: Mapping[str, float] | tuple[tuple[str, float], ...] = ()#