ranklens.explain¶
ranklens.explain
¶
Explanations of a ranking model: why one document is above another, what moves the metric.
EXACT_MAX_FEATURES
module-attribute
¶
EXACT_MAX_FEATURES = 12
2^12 = 4096 scored points: still one cheap call; beyond it orders are sampled.
CallableAdapter
¶
Any scoring function features -> scores: the escape hatch for every other model.
Source code in src/ranklens/explain/adapters/base.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
ScorerAdapter
¶
Bases: Protocol
A ranking model seen through the three things explanation needs.
Source code in src/ranklens/explain/adapters/base.py
22 23 24 25 26 27 28 29 30 31 32 33 | |
native_importance
¶
native_importance() -> Mapping[str, float] | None
The model's own importance per feature, or None when it has none.
Source code in src/ranklens/explain/adapters/base.py
31 32 33 | |
QueryFeatures
dataclass
¶
features[i] belongs to docs[i]; judgements may cover only some of them.
The matrix is copied and made read-only: explanations shuffle copies of it, never it.
Source code in src/ranklens/explain/data.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
from_rows
classmethod
¶
from_rows(
query_id: QueryId,
docs: Sequence[DocId],
rows: Sequence[Sequence[float]],
judgements: Mapping[DocId, float],
columns: Sequence[int] | None = None,
) -> QueryFeatures
From plain rows (as a feature table reader gives them), keeping columns.
Source code in src/ranklens/explain/data.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
adapt
¶
adapt(
model: Any, feature_names: Sequence[str] | None = None
) -> ScorerAdapter
The adapter for model, found by the packages of the classes in its MRO.
Walking the MRO recognizes subclasses defined elsewhere (a user's class derived from
LGBMRanker) and needs none of the libraries imported. An adapter is returned
as is; anything unknown is an UnsupportedModelError that points to CallableAdapter.
Source code in src/ranklens/explain/adapters/__init__.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
correlated_groups
¶
correlated_groups(
queries: Iterable[QueryFeatures],
feature_names: Sequence[str],
*,
threshold: float = 0.8,
) -> tuple[tuple[str, ...], ...]
Groups of two or more features linked by |correlation| >= threshold.
Correlation is computed over all documents of all queries, pairwise over the documents
where both values are present (nan is a missing value). Features in no group are
not listed; a constant feature correlates with nothing.
Source code in src/ranklens/explain/grouping.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
load_model
¶
load_model(
path: str | Path,
*,
kind: str | None = None,
allow_pickle: bool = False,
) -> Any
The model saved at path, ready for ranklens.explain.adapt.
Source code in src/ranklens/explain/loading.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
ranklens.explain.adapters
¶
Model adapters: one protocol for every model the explanations work with.
Scores
module-attribute
¶
Scores = npt.NDArray[np.float64]
One score per document; higher ranks first.
ADAPTERS
module-attribute
¶
ADAPTERS: dict[str, Callable[..., ScorerAdapter]] = {
"catboost": CatBoostAdapter,
"lightgbm": LightGBMAdapter,
"xgboost": XGBoostAdapter,
"sklearn": SklearnAdapter,
"torch": TorchAdapter,
"onnxruntime": ONNXAdapter,
}
Top-level package of a model's class -> its adapter. A new library is one line here.
CallableAdapter
¶
Any scoring function features -> scores: the escape hatch for every other model.
Source code in src/ranklens/explain/adapters/base.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
ScorerAdapter
¶
Bases: Protocol
A ranking model seen through the three things explanation needs.
Source code in src/ranklens/explain/adapters/base.py
22 23 24 25 26 27 28 29 30 31 32 33 | |
native_importance
¶
native_importance() -> Mapping[str, float] | None
The model's own importance per feature, or None when it has none.
Source code in src/ranklens/explain/adapters/base.py
31 32 33 | |
CatBoostAdapter
¶
Bases: _Adapter
CatBoost ranker, regressor or classifier: raw formula value.
Source code in src/ranklens/explain/adapters/libraries.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
LightGBMAdapter
¶
Bases: _Adapter
LightGBM Booster or its scikit-learn wrappers: raw score, gain importance.
Source code in src/ranklens/explain/adapters/libraries.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
SklearnAdapter
¶
Bases: _Adapter
A fitted scikit-learn estimator; importance if it has one.
Regressors score with predict; binary classifiers with decision_function
or, without it, the probability of the positive class.
Source code in src/ranklens/explain/adapters/libraries.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
XGBoostAdapter
¶
Bases: _Adapter
XGBoost Booster or its scikit-learn wrappers: margin, gain importance.
Source code in src/ranklens/explain/adapters/libraries.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
ONNXAdapter
¶
An onnxruntime.InferenceSession with one input of documents by features.
The first output is the score. The number of features comes from the input shape; names default to f0, f1, … as for other models trained without names.
Source code in src/ranklens/explain/adapters/neural.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
TorchAdapter
¶
A torch.nn.Module mapping documents by features to one score per document.
Scored in inference mode without changing the module: eval() is applied for the
call (dropout off, batch norm frozen) and every submodule gets its mode back after it.
A module knows no feature names, so they are required.
Source code in src/ranklens/explain/adapters/neural.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
adapt
¶
adapt(
model: Any, feature_names: Sequence[str] | None = None
) -> ScorerAdapter
The adapter for model, found by the packages of the classes in its MRO.
Walking the MRO recognizes subclasses defined elsewhere (a user's class derived from
LGBMRanker) and needs none of the libraries imported. An adapter is returned
as is; anything unknown is an UnsupportedModelError that points to CallableAdapter.
Source code in src/ranklens/explain/adapters/__init__.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |