models.partial_dependence()
Partial dependence: the mean prediction with feature set to each
Usage
models.partial_dependence(
model, frame, build, feature, values, y=None, splits=None, n_jobs=None
)value on every row and every other column as it is (CAS Monograph 16, section 9.3). An average over the portfolio, so an effect that differs by another feature (an interaction) is blended across it.
Parameters
model: object-
A fitted model with
predict(design); withsplits, a model withfit(design, y), explained on each split’s test rows. frame: dict-
As for permutation_importance.
build: callable-
build(frame) -> Design. feature: str-
One column of
frame. values: list-
The values to set it to.
y: list of float = None-
The response, needed only with
splitsto fit. splits: list of (list of int, list of int) = Nonen_jobs: int = None
Returns
PartialDependence
Examples
>>> import math
>>> from prospicio.models import Design, Glm, partial_dependence
>>> x = [float(i % 4) for i in range(40)]
>>> z = [float(i % 3) for i in range(40)]
>>> y = [math.exp(0.2 * u + 0.1 * v) * (1.1 if i % 2 else 0.9) for i, (u, v) in enumerate(zip(x, z))]
>>> build = lambda f: Design([[1.0] * len(f["x"]), list(f["x"]), list(f["z"])], ["(Intercept)", "x", "z"])
>>> fit = Glm("poisson").fit(build({"x": x, "z": z}), y)
>>> pd = partial_dependence(fit, {"x": x, "z": z}, build, "x", [0.0, 1.0])
>>> round(pd.relative(0.0)[1] / math.exp(fit.coefficients[1]), 12)1.0