patsy.builtins
API reference¶
This module defines some tools that are automatically made available
to code evaluated in formulas. You can also access it directly; use
from patsy.builtins import *
to import the same variables that
formula code receives automatically.
-
patsy.builtins.
I
(x)¶ The identity function. Simply returns its input unchanged.
Since Patsy’s formula parser ignores anything inside a function call syntax, this is useful to ‘hide’ arithmetic operations from it. For instance:
y ~ x1 + x2
has
x1
andx2
as two separate predictors. But in:y ~ I(x1 + x2)
we instead have a single predictor, defined to be the sum of
x1
andx2
.
-
patsy.builtins.
Q
(name)¶ A way to ‘quote’ variable names, especially ones that do not otherwise meet Python’s variable name rules.
If
x
is a variable,Q("x")
returns the value ofx
. (Note thatQ
takes the string"x"
, not the value ofx
itself.) This works even if instead ofx
, we have a variable name that would not otherwise be legal in Python.For example, if you have a column of data named
weight.in.kg
, then you can’t write:y ~ weight.in.kg
because Python will try to find a variable named
weight
, that has an attribute namedin
, that has an attribute namedkg
. (And worse yet,in
is a reserved word, which makes this example doubly broken.) Instead, write:y ~ Q("weight.in.kg")
and all will be well. Note, though, that this requires embedding a Python string inside your formula, which may require some care with your quote marks. Some standard options include:
my_fit_function("y ~ Q('weight.in.kg')", ...) my_fit_function('y ~ Q("weight.in.kg")', ...) my_fit_function("y ~ Q(\"weight.in.kg\")", ...)
Note also that
Q
is an ordinary Python function, which means that you can use it in more complex expressions. For example, this is a legal formula:y ~ np.sqrt(Q("weight.in.kg"))
-
class
patsy.builtins.
ContrastMatrix
(matrix, column_suffixes)¶ A simple container for a matrix used for coding categorical factors.
Attributes:
-
matrix
¶ A 2d ndarray, where each column corresponds to one column of the resulting design matrix, and each row contains the entries for a single categorical variable level. Usually n-by-n for a full rank coding or n-by-(n-1) for a reduced rank coding, though other options are possible.
-
column_suffixes
¶ A list of strings to be appended to the factor name, to produce the final column names. E.g. for treatment coding the entries will look like
"[T.level1]"
.
-
-
class
patsy.builtins.
Treatment
(reference=None)¶ Treatment coding (also known as dummy coding).
This is the default coding.
For reduced-rank coding, one level is chosen as the “reference”, and its mean behaviour is represented by the intercept. Each column of the resulting matrix represents the difference between the mean of one level and this reference level.
For full-rank coding, classic “dummy” coding is used, and each column of the resulting matrix represents the mean of the corresponding level.
The reference level defaults to the first level, or can be specified explicitly.
# reduced rank In [1]: dmatrix("C(a, Treatment)", balanced(a=3)) Out[1]: DesignMatrix with shape (3, 3) Intercept C(a, Treatment)[T.a2] C(a, Treatment)[T.a3] 1 0 0 1 1 0 1 0 1 Terms: 'Intercept' (column 0) 'C(a, Treatment)' (columns 1:3) # full rank In [2]: dmatrix("0 + C(a, Treatment)", balanced(a=3))