File: API-reference.rst

package info (click to toggle)
patsy 0.4.1%2Bgit34-ga5b54c2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,444 kB
  • ctags: 884
  • sloc: python: 8,797; makefile: 130; sh: 15
file content (247 lines) | stat: -rw-r--r-- 5,658 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
``patsy`` API reference
==========================

This is a complete reference for everything you get when you `import
patsy`.

.. module:: patsy

.. ipython:: python
   :suppress:

   from patsy import *

Basic API
---------

.. autofunction:: dmatrix
.. autofunction:: dmatrices

.. autofunction:: incr_dbuilders
.. autofunction:: incr_dbuilder

.. autoexception:: PatsyError
   :members:

Convenience utilities
---------------------

.. autofunction:: balanced

.. autofunction:: demo_data

Design metadata
---------------

.. autoclass:: DesignInfo

   Here's an example of the most common way to get a :class:`DesignInfo`:

   .. ipython:: python

      mat = dmatrix("a + x", demo_data("a", "x", nlevels=3))
      di = mat.design_info

   .. attribute:: column_names

      The names of each column, represented as a list of strings in
      the proper order. Guaranteed to exist.

      .. ipython:: python

         di.column_names

   .. attribute:: column_name_indexes

      An :class:`~collections.OrderedDict` mapping column names (as
      strings) to column indexes (as integers). Guaranteed to exist
      and to be sorted from low to high.

      .. ipython:: python

         di.column_name_indexes

   .. attribute:: term_names

      The names of each term, represented as a list of strings in
      the proper order. Guaranteed to exist. There is a one-to-many
      relationship between columns and terms -- each term generates
      one or more columns.

      .. ipython:: python

         di.term_names

   .. attribute:: term_name_slices

      An :class:`~collections.OrderedDict` mapping term names (as
      strings) to Python :func:`slice` objects indicating which
      columns correspond to each term. Guaranteed to exist. The slices
      are guaranteed to be sorted from left to right and to cover the
      whole range of columns with no overlaps or gaps.

      .. ipython:: python

         di.term_name_slices

   .. attribute:: terms

      A list of :class:`Term` objects representing each term. May be
      None, for example if a user passed in a plain preassembled
      design matrix rather than using the Patsy machinery.

      .. ipython:: python

         di.terms
         [term.name() for term in di.terms]

   .. attribute:: term_slices

      An :class:`~collections.OrderedDict` mapping :class:`Term`
      objects to Python :func:`slice` objects indicating which columns
      correspond to which terms. Like :attr:`terms`, this may be None.

      .. ipython:: python

         di.term_slices

   .. attribute:: factor_infos

      A dict mapping factor objects to :class:`FactorInfo` objects
      providing information about each factor. Like :attr:`terms`,
      this may be None.

      .. ipython:: python

         di.factor_infos

   .. attribute:: term_codings

      An :class:`~collections.OrderedDict` mapping each :class:`Term`
      object to a list of :class:`SubtermInfo` objects which together
      describe how this term is encoded in the final design
      matrix. Like :attr:`terms`, this may be None.

      .. ipython:: python

         di.term_codings

   .. attribute:: builder

      In versions of patsy before 0.4.0, this returned a
      ``DesignMatrixBuilder`` object which could be passed to
      :func:`build_design_matrices`. Starting in 0.4.0,
      :func:`build_design_matrices` now accepts :class:`DesignInfo`
      objects directly, and writing ``f(design_info.builder)`` is now a
      deprecated alias for simply writing ``f(design_info)``.

   A number of convenience methods are also provided that take
   advantage of the above metadata:

   .. automethod:: describe

   .. automethod:: linear_constraint

   .. automethod:: slice

   .. automethod:: subset

   .. automethod:: from_array

.. autoclass:: FactorInfo

.. autoclass:: SubtermInfo

.. autoclass:: DesignMatrix

   .. automethod:: __new__

.. _stateful-transforms-list:

Stateful transforms
-------------------

Patsy comes with a number of :ref:`stateful transforms
<stateful-transforms>` built in:

.. autofunction:: center

.. autofunction:: standardize

.. function:: scale(x, center=True, rescale=True, ddof=0)

   An alias for :func:`standardize`, for R compatibility.

Finally, this is not itself a stateful transform, but it's useful if
you want to define your own:

.. autofunction:: stateful_transform

.. _categorical-coding-ref:

Handling categorical data
-------------------------

.. autoclass:: Treatment
.. autoclass:: Diff
.. autoclass:: Poly
.. autoclass:: Sum
.. autoclass:: Helmert

.. autoclass:: ContrastMatrix

Spline regression
-----------------

.. autofunction:: bs
.. autofunction:: cr
.. autofunction:: cc
.. autofunction:: te

Working with formulas programmatically
--------------------------------------

.. autoclass:: Term

.. data:: INTERCEPT

   This is a pre-instantiated zero-factors :class:`Term` object
   representing the intercept, useful for making your code clearer. Do
   remember though that this is not a singleton object, i.e., you
   should compare against it using ``==``, not ``is``.

.. autoclass:: LookupFactor

.. autoclass:: EvalFactor

.. autoclass:: ModelDesc

Working with the Python execution environment
---------------------------------------------

.. autoclass:: EvalEnvironment
   :members:

Building design matrices
------------------------

.. autofunction:: design_matrix_builders

.. autofunction:: build_design_matrices

Missing values
--------------

.. autoclass:: NAAction
   :members:

Linear constraints
------------------

.. autoclass:: LinearConstraint

Origin tracking
---------------

.. autoclass:: Origin
   :members: