1.2.2.1.5. patsy.DesignInfo.slice

DesignInfo.slice(columns_specifier)[source]

Locate a subset of design matrix columns, specified symbolically.

A patsy design matrix has two levels of structure: the individual columns (which are named), and the terms in the formula that generated those columns. This is a one-to-many relationship: a single term may span several columns. This method provides a user-friendly API for locating those columns.

(While we talk about columns here, this is probably most useful for indexing into other arrays that are derived from the design matrix, such as regression coefficients or covariance matrices.)

The columns_specifier argument can take a number of forms:

  • A term name
  • A column name
  • A Term object
  • An integer giving a raw index
  • A raw slice object

In all cases, a Python slice() object is returned, which can be used directly for indexing.

Example:

y, X = dmatrices("y ~ a", demo_data("y", "a", nlevels=3))
betas = np.linalg.lstsq(X, y)[0]
a_betas = betas[X.design_info.slice("a")]

(If you want to look up a single individual column by name, use design_info.column_name_indexes[name].)