cleverly.plotting.learners#

cleverly.plotting.learners(source: Any, *, regimen: str | None = None, horizon: int | None = None, cause: str | None = None, time_label: str = 'Time point', title: str | Literal[False] | None = None, subtitle: str | Literal[False] | None = None, xlabel: str | Sequence[str] | None = None, ylabel: str | Sequence[str] | None = None, legend: str | Literal[False] = 'top', figsize: tuple[float, float] | None = None, ax: Axes | None = None) → Figure[source]#

Plot which learners carry a fit.

A SuperLearnerDiagnostics draws two panels with the candidates in the same order: each candidate’s cross-validated risk (lower is better) and its weight in the ensemble. Use it to see whether one learner dominates and which ones fail. The ensemble’s own risk is not drawn, because the stored value is scored on the predictions its weights were fitted to, so it flatters the ensemble.

A fitted result, its result.diagnostics.nuisance_models() or one NuisanceModelReport draws the same two panels for every nuisance model a Super Learner fitted, one row of panels per model, from the risks and weights the reports hold (each the mean over the model’s cross-fitting folds).

A CTMLESelection (or a collaborative result that holds one) draws the cross-validated risk along the candidate sequence with the chosen candidate marked, which shows whether the choice was sharp. A survival collaborative fit draws the same for each arm and horizon, along its path of moves.

A longitudinal fit, or its diagnostics.nuisance_models(), draws one column of panels per nuisance role that a Super Learner fitted: the ensemble weights stacked at each time point above, each candidate’s cross-validated risk below.

A continuous-time survival fit, or its diagnostics.nuisance_models(), draws one panel per hazard (each cause, then censoring) and one for the treatment model: each candidate’s cross-validated loss, with the candidate the fit chose marked. Each selection keeps one candidate, so no weights are drawn. A cross-fitted fit draws the first fold’s selections, the ones it keeps.

Parameters:
sourceSuperLearnerDiagnostics or CTMLESelection or a fitted result

What to plot: one of the first two, or a TMLEResult, LongitudinalResult or ContinuousSurvivalResult. A fitted SuperLearner holds the first as diagnostics_. A NuisanceModelReport, a fit’s NuisanceDiagnostics and a longitudinal or continuous-time survival fit’s diagnostics.nuisance_models() are accepted too.

regimenstr or None, default=None

On a longitudinal fit or its nuisance report, the regimen whose outcome regressions are drawn; the first when omitted. The treatment and censoring models serve every regimen and are always drawn.

horizonint or None, default=None

The horizon of those outcome regressions on a fit with several; the largest when omitted.

causestr or None, default=None

Their cause under competing events; the first when omitted.

time_labelstr, default=”Time point”

Words for a time point on a longitudinal fit’s panels.

titlestr, False or None, default=None

The title; a short description of the figure when omitted, and none when False.

subtitlestr, False or None, default=None

The line under the title; the plot’s own, which names the estimate and its headline number, when omitted, and none when False.

xlabelstr, sequence of str or None, default=None

The x-axis label. A string replaces the first labelled panel’s label and the same label wherever it repeats; a sequence gives each labelled panel its own, in reading order.

ylabelstr, sequence of str or None, default=None

The y-axis label, given as for xlabel.

legend{“top”, “right”, “bottom”} or False, default=”top”

Where the key goes: above the axes, right of them, or under the figure; False draws no key.

figsizetuple of float or None, default=None

Width and height in inches of a figure the plot creates.

axAxes or None, default=None

Axes to draw on. Only for a collaborative selection, which is one panel.

Returns:
Figure

Two panels per Super Learner, one for a collaborative selection, one per arm and horizon for a survival collaborative fit, two per role for a longitudinal fit, one per selected model for a continuous-time survival fit.

Raises:
ValueError

When the source holds nothing to draw (no nuisance model was fitted by a Super Learner), when ax is given for a figure of several panels, or when a continuous-time survival fit’s report names a chosen candidate that its library lists more than once.

TypeError

When the source is none of the above.

CapabilityError

When the source is an adaptive TMLE fit or its nuisance report, which keep no candidate risks or weights.

DataError

When the regimen, horizon or cause is not in a longitudinal report.

See also

cleverly.plotting.calibration

Whether the models’ probabilities are calibrated.

Notes

The figure carries no note text. It does not mark an experimental fit or band, one whose interval has no checked coverage because no registered study covers it or a stated condition applies, such as too few clusters: read notes() on the figure before you publish it. For this plot the notes say the risks are descriptive and what the criterion adds to the loss. On a longitudinal fit they also say that the weights are averaged over folds, which draw and which regimen’s outcome regressions are drawn. On a continuous-time survival fit they say which fold’s selections are drawn and which model has no panel.

Examples

>>> import matplotlib
>>> matplotlib.use("Agg")
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from sklearn.linear_model import LinearRegression, Ridge
>>> from cleverly.learners import SuperLearner
>>> from cleverly.plotting import learners
>>> rng = np.random.default_rng(0)
>>> x = rng.normal(size=(120, 2))
>>> y = x[:, 0] + rng.normal(size=120)
>>> ensemble = SuperLearner(
...     library=[("ols", LinearRegression()), ("ridge", Ridge(alpha=5.0))],
...     task="regression",
...     n_folds=3,
... ).fit(x, y)
>>> fig = learners(ensemble.diagnostics_)
>>> fig.texts[0].get_text()
'Learner risks and weights'
>>> plt.close(fig)