Skip to main content

models.MarsFit

The knots MARS selected, from mars: each term as

Usage

Source

models.MarsFit()

(name, knot, direction), with direction "up" for max(0, x - k), "down" for max(0, k - x) and "linear" for x.

Attributes

Name Description
coefficients The intercept and each selected term’s coefficient.
forward_terms The forward pass’s terms, before the backward pass.
gcv The GCV of the selected terms.
grsq 1 - gcv / gcv0.
names The predictors’ names.
rsq 1 - rss / rss0.
rss The (weighted) residual sum of squares, or the GLM’s deviance.
steps The forward pass step by step: the terms each added, and the RSS
termination Why the forward pass stopped: "no_improvement",
terms The selected terms (name, knot, direction), in the order the

coefficients

The intercept and each selected term’s coefficient.

coefficients: list[float]


forward_terms

The forward pass’s terms, before the backward pass.

forward_terms: list[tuple[str, float, str]]


gcv

The GCV of the selected terms.

gcv: float


grsq

1 - gcv / gcv0.

grsq: float


names

The predictors’ names.

names: list[str]


rsq

1 - rss / rss0.

rsq: float


rss

The (weighted) residual sum of squares, or the GLM’s deviance.

rss: float


steps

The forward pass step by step: the terms each added, and the RSS

steps: list[dict]

(or deviance), R² and GCV after it.


termination

Why the forward pass stopped: "no_improvement",

termination: str

"threshold", "gcv", "rsq" or "max_terms".


terms

The selected terms (name, knot, direction), in the order the

terms: list[tuple[str, float, str]]

forward pass added them, without the intercept.

Methods

Name Description
add_terms() terms with each selected predictor added: numeric when
from_json() Reads an artifact written by to_json.
knots() The sorted knots of name’s hinges, for Terms.hinges.
linear() Whether name enters linearly besides its hinges.
to_json() The fit as a versioned JSON artifact: the names, the selected and

add_terms()

terms with each selected predictor added: numeric when

Usage

Source

add_terms(terms)

linear and hinges at its knots, spanning the selected terms.

Parameters
terms: Terms
Returns
Terms

from_json()

Reads an artifact written by to_json.

Usage

Source

from_json(text)
Parameters
text: str
Returns
MarsFit
Raises
ValueError
For malformed JSON, another format, a newer format version or inconsistent fields.

knots()

The sorted knots of name’s hinges, for Terms.hinges.

Usage

Source

knots(name)

linear()

Whether name enters linearly besides its hinges.

Usage

Source

linear(name)

to_json()

The fit as a versioned JSON artifact: the names, the selected and

Usage

Source

to_json()

forward terms, the forward pass’s steps, the coefficients and the fit statistics, with the GLM fitted on the terms when family was given; numbers bit for bit (#978). from_json reads it back; pickle uses the same document.

Returns
str
Examples
>>> import pickle
>>> from prospicio.models import MarsFit, mars
>>> x = [float(i) for i in range(60)]
>>> fit = mars({"x": x}, [1 + 0.5 * v + 3 * max(0.0, v - 31) for v in x])
>>> MarsFit.from_json(fit.to_json()).knots("x")

[31.0]

>>> pickle.loads(pickle.dumps(fit)).coefficients == fit.coefficients

True