Skip to main content

models.Terms

The terms of a model: an intercept, numeric columns, factors,

Usage

Source

models.Terms()

interactions, polynomials, hinges, steps, bins, logs and ordinals.

Build them up, then fit them to training data to learn the factor levels; the result builds the same design matrix on any data.

Examples

>>> from prospicio.models import Terms
>>> data = {"age": [30.0, 45.0, 60.0], "region": ["N", "S", "W"]}
>>> coding = Terms().intercept().numeric("age").factor("region").fit(data)
>>> coding.names

[‘(Intercept)’, ‘age’, ‘region[S]’, ‘region[W]’]

Methods

Name Description
bins() Adds a numeric column cut into bins (-inf, c1], (c1, c2], ...,</td> </tr> <tr> <td><a href="#factor" class="doc-function doc-label doc-label-method">factor()</a></td> <td>Adds a factor in treatment coding.</td> </tr> <tr> <td><a href="#fit" class="doc-function doc-label doc-label-method">fit()</a></td> <td>Learns factor levels from training data.</td> </tr> <tr> <td><a href="#from_json" class="doc-function doc-label doc-label-method">from_json()</a></td> <td>Reads an artifact written byto_json.</td> </tr> <tr> <td><a href="#hinges" class="doc-function doc-label doc-label-method">hinges()</a></td> <td>Adds hinge functionsmax(0, x - k)at each knot, named</td> </tr> <tr> <td><a href="#interaction" class="doc-function doc-label doc-label-method">interaction()</a></td> <td>Adds the interaction of two columns: the product of their coded</td> </tr> <tr> <td><a href="#intercept" class="doc-function doc-label doc-label-method">intercept()</a></td> <td>Adds an intercept.</td> </tr> <tr> <td><a href="#log" class="doc-function doc-label doc-label-method">log()</a></td> <td>Addslog(x / base), namedlog(x/base): a log transform</td> </tr> <tr> <td><a href="#natural_spline" class="doc-function doc-label doc-label-method">natural_spline()</a></td> <td>Adds a natural cubic spline with interiorknots, linear beyond</td> </tr> <tr> <td><a href="#numeric" class="doc-function doc-label doc-label-method">numeric()</a></td> <td>Adds a numeric column.</td> </tr> <tr> <td><a href="#ordinal" class="doc-function doc-label doc-label-method">ordinal()</a></td> <td>Adds an ordered factor in cumulative coding: for each level after</td> </tr> <tr> <td><a href="#orthogonal_poly" class="doc-function doc-label doc-label-method">orthogonal_poly()</a></td> <td>Adds orthogonal polynomials of degree 1 todegreeon the</td> </tr> <tr> <td><a href="#poly" class="doc-function doc-label doc-label-method">poly()</a></td> <td>Adds raw powersx, x2, …, xdegreeof a numeric column,</td> </tr> <tr> <td><a href="#steps" class="doc-function doc-label doc-label-method">steps()</a></td> <td>Adds steps1[x >= k]at each knot, namedstep(x,k)``.
to_json() The terms as a versioned JSON artifact (format

bins()

Adds a numeric column cut into bins ``(-inf, c1], (c1, c2], …,

Usage

Source

bins(name, cuts, reference=None)

(ck, inf)and coded as a factor, named as R'scut(x[(30,45]]``).

Parameters
name: str
cuts: list of float

Strictly increasing.

reference: int = None
Index of the reference bin; the lowest by default.
Returns
Terms

factor()

Adds a factor in treatment coding.

Usage

Source

factor(name, reference=None)
Parameters
name: str
reference: str = None
Reference level; the first in sorted order by default.
Returns
Terms

fit()

Learns factor levels from training data.

Usage

Source

fit(data)
Parameters
data: dict of str to list
Numeric columns as lists of numbers, factors as lists of strings.
Returns
Coding
Raises
ValueError
If a column is missing or has the wrong kind.

from_json()

Reads an artifact written by to_json.

Usage

Source

from_json(text)
Parameters
text: str
Returns
Terms
Raises
ValueError
For malformed JSON, another format, a newer format version or an invalid term.

hinges()

Adds hinge functions max(0, x - k) at each knot, named

Usage

Source

hinges(name, knots)

hinge(x,k).

Parameters
name: str
knots: list of float
Strictly increasing.
Returns
Terms

interaction()

Adds the interaction of two columns: the product of their coded

Usage

Source

interaction(a, b)

columns, each numeric as is or a factor in treatment coding (first level as reference), named a:b, a[x]:b or a[x]:b[y], as R’s a:b beside main effects (CAS Monograph 5, section 5.6).

Parameters
a: str
b: str
Returns
Terms

intercept()

Adds an intercept.

Usage

Source

intercept()
Returns
Terms

log()

Adds log(x / base), named log(x/base): a log transform

Usage

Source

log(name, base)

centred at a base value (CAS Monograph 5, section 5.6.2).

Parameters
name: str
base: float
Positive.
Returns
Terms

natural_spline()

Adds a natural cubic spline with interior knots, linear beyond

Usage

Source

natural_spline(name, knots, boundary=None)

the boundary knots (the training range unless given): len(knots) + 1 columns named spline(x)1, …. The basis is the restricted cubic one, which spans the same functions as R’s splines::ns(x, knots): fitted values agree, coefficients differ.

Parameters
name: str
knots: list of float

Interior knots, strictly increasing.

boundary: tuple of float = None
The boundary knots; the training minimum and maximum by default.
Returns
Terms

numeric()

Adds a numeric column.

Usage

Source

numeric(name)
Parameters
name: str
Returns
Terms

ordinal()

Adds an ordered factor in cumulative coding: for each level after

Usage

Source

ordinal(name, levels)

the first, 1[level >= it], named x[>=level], so each coefficient is the step from the level below.

Parameters
name: str
levels: list of str
The levels in order, lowest first.
Returns
Terms
Examples
>>> from prospicio.models import Terms
>>> data = {"size": ["small", "large", "medium"]}
>>> Terms().ordinal("size", ["small", "medium", "large"]).fit(data).names

[‘size[>=medium]’, ‘size[>=large]’]


orthogonal_poly()

Adds orthogonal polynomials of degree 1 to degree on the

Usage

Source

orthogonal_poly(name, degree)

training values, as R’s poly(x, degree), named poly(x)1, …; the recurrence learned at fit rebuilds them on new data.

Parameters
name: str
degree: int
Returns
Terms

poly()

Adds raw powers x, x**2, ..., x**degree of a numeric column,

Usage

Source

poly(name, degree)

named x, x^2, ….

Parameters
name: str
degree: int
Returns
Terms

steps()

Adds steps 1[x >= k] at each knot, named step(x,k).

Usage

Source

steps(name, knots)
Parameters
name: str
knots: list of float
Strictly increasing.
Returns
Terms

to_json()

The terms as a versioned JSON artifact (format

Usage

Source

to_json()

risk_rs.terms), numbers bit for bit (#978). from_json reads it back; pickle uses the same document.

Returns
str
Examples
>>> import pickle
>>> from prospicio.models import Terms
>>> terms = Terms().intercept().numeric("age").factor("region")
>>> Terms.from_json(terms.to_json()).to_json() == terms.to_json()

True

>>> pickle.loads(pickle.dumps(terms)).to_json() == terms.to_json()

True