Skip to main content

models.ale()

First-order accumulated local effects (Apley and Zhu, 2020), as R’s

Usage

Source

models.ale(
    model,
    frame,
    build,
    feature,
    bins=40,
    levels=None,
)

ALEPlot: the effect of feature built from prediction differences across small intervals of it, each over only the rows that fall in the interval, so correlated features are not pushed into combinations the data never shows (partial dependence’s weakness).

A numeric feature is cut at its minimum and its type 1 quantiles at 1/bins, ..., 1 (duplicates merged); each interval’s local effect is the mean of f(z_k) - f(z_{k-1}) over its rows, accumulated and then centred so the mean effect over the rows is 0 (ALEPlot’s trapezoid rule). A categorical feature’s levels are ordered as levels gives or, by default, as ALEPlot orders them (classical scaling of the distances between the levels’ distributions of the other columns, read from the end whose level sorts first); the local effect between neighbouring levels averages the moves of the rows on both sides, and the effects are centred on the level frequencies.

Parameters

model: object

A fitted model with predict(design).

frame: dict

Column name to values; every column but feature enters a categorical feature’s default order.

build: callable

build(frame) -> Design.

feature: str
bins: int = 40

For a numeric feature, the number of quantile intervals K.

levels: list = None
For a categorical feature, the order of its levels.

Returns

AccumulatedLocalEffects

Examples

>>> from prospicio.models import Design, Glm, ale
>>> f = {"x": [i / 10 for i in range(50)]}
>>> build = lambda f: Design([[1.0] * len(f["x"]), list(f["x"])], ["(Intercept)", "x"])
>>> fit = Glm("gaussian").fit(build(f), [2.0 * v + (i % 3) for i, v in enumerate(f["x"])])
>>> a = ale(fit, f, build, "x", bins=5)
>>> round((a.effects[-1] - a.effects[0]) / (a.values[-1] - a.values[0]) / fit.coefficients[1], 12)

1.0