/usr/share/doc/python-patsy-doc/html/library-developers.html is in python-patsy-doc 0.4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Using Patsy in your library — patsy 0.4.1 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/facebox.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.4.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/show-code.js"></script>
<script type="text/javascript" src="_static/facebox.js"></script>
<link rel="top" title="patsy 0.4.1 documentation" href="index.html" />
<link rel="next" title="Differences between R and Patsy formulas" href="R-comparison.html" />
<link rel="prev" title="Model specification for experts and computers" href="expert-model-specification.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="R-comparison.html" title="Differences between R and Patsy formulas"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="expert-model-specification.html" title="Model specification for experts and computers"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">patsy 0.4.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="using-patsy-in-your-library">
<span id="library-developers"></span><h1>Using Patsy in your library<a class="headerlink" href="#using-patsy-in-your-library" title="Permalink to this headline">¶</a></h1>
<p>Our goal is to make Patsy the de facto standard for describing
models in Python, regardless of the underlying package in use – just
as formulas are the standard interface to all R packages. Therefore
we’ve tried to make it as easy as possible for you to build Patsy
support into your libraries.</p>
<p>Patsy is a good houseguest:</p>
<ul class="simple">
<li>Pure Python, no compilation necessary.</li>
<li>Exhaustive tests (>98% statement coverage at time of writing) and
documentation (you’re looking at it).</li>
<li>No dependencies besides numpy.</li>
<li>Tested and supported on every version of Python since 2.5. (And 2.4
probably still works too if you really want it, it’s just become too
hard to keep a working 2.4 environment on the test server.)</li>
</ul>
<p>So you can be pretty confident that adding a dependency on Patsy
won’t create much hassle for your users.</p>
<p>And, of course, the fundamental design is very conservative – the
formula mini-language in S was first described in Chambers and Hastie
(1992), more than two decades ago. It’s still in heavy use today in R,
which is one of the most popular environments for statistical
programming. Many of your users may already be familiar with it. So we
can be pretty certain that it will hold up to real-world usage.</p>
<div class="section" id="using-the-high-level-interface">
<h2>Using the high-level interface<a class="headerlink" href="#using-the-high-level-interface" title="Permalink to this headline">¶</a></h2>
<p>If you have a function whose signature currently looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">mymodel2</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
<p>or this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">mymodel1</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span>
<span class="o">...</span>
</pre></div>
</div>
<p>then adding Patsy support is extremely easy (though of course like
any other API change, you may have to deprecate the old interface, or
provide two interfaces in parallel, depending on your situation). Just
write something like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">mymodel2_patsy</span><span class="p">(</span><span class="n">formula_like</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="p">{},</span> <span class="o">...</span><span class="p">):</span>
<span class="n">y</span><span class="p">,</span> <span class="n">X</span> <span class="o">=</span> <span class="n">patsy</span><span class="o">.</span><span class="n">dmatrices</span><span class="p">(</span><span class="n">formula_like</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="o">...</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">mymodel1_patsy</span><span class="p">(</span><span class="n">formula_like</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="p">{},</span> <span class="o">...</span><span class="p">):</span>
<span class="n">X</span> <span class="o">=</span> <span class="n">patsy</span><span class="o">.</span><span class="n">dmatrix</span><span class="p">(</span><span class="n">formula_like</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="o">...</span>
</pre></div>
</div>
<p>(See <a class="reference internal" href="API-reference.html#patsy.dmatrices" title="patsy.dmatrices"><code class="xref py py-func docutils literal"><span class="pre">dmatrices()</span></code></a> and <a class="reference internal" href="API-reference.html#patsy.dmatrix" title="patsy.dmatrix"><code class="xref py py-func docutils literal"><span class="pre">dmatrix()</span></code></a> for details.) This won’t
force your users to switch to formulas immediately; they can replace
code that looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">build_matrices_laboriously</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">mymodel2</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="n">other_result</span> <span class="o">=</span> <span class="n">mymodel1</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>with code like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">build_matrices_laboriously</span><span class="p">()</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">mymodel2</span><span class="p">((</span><span class="n">y</span><span class="p">,</span> <span class="n">X</span><span class="p">),</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="n">other_result</span> <span class="o">=</span> <span class="n">mymodel1</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>Of course in the long run they might want to throw away that
<code class="xref py py-func docutils literal"><span class="pre">build_matrices_laboriously()</span></code> function and start using formulas,
but they aren’t forced to just to start using your new interface.</p>
<div class="section" id="working-with-metadata">
<h3>Working with metadata<a class="headerlink" href="#working-with-metadata" title="Permalink to this headline">¶</a></h3>
<p>Once you’ve started using Patsy to handle formulas, you’ll probably
want to take advantage of the metadata that Patsy provides, so that
you can display regression coefficients by name and so forth. Design
matrices processed by Patsy always have a <code class="docutils literal"><span class="pre">.design_info</span></code>
attribute which contains lots of information about the design: see
<a class="reference internal" href="API-reference.html#patsy.DesignInfo" title="patsy.DesignInfo"><code class="xref py py-class docutils literal"><span class="pre">DesignInfo</span></code></a> for details.</p>
</div>
<div class="section" id="predictions">
<h3>Predictions<a class="headerlink" href="#predictions" title="Permalink to this headline">¶</a></h3>
<p>Another nice feature is making predictions on new data. But this
requires that we can take in new data, and transform it to create a
new <cite>X</cite> matrix. Or if we want to compute the likelihood of our model
on new data, we need both new <cite>X</cite> and <cite>y</cite> matrices.</p>
<p>This is also easily done with Patsy – first fetch the relevant
<a class="reference internal" href="API-reference.html#patsy.DesignInfo" title="patsy.DesignInfo"><code class="xref py py-class docutils literal"><span class="pre">DesignInfo</span></code></a> objects by doing <code class="docutils literal"><span class="pre">input_data.design_info</span></code>, and
then pass them to <a class="reference internal" href="API-reference.html#patsy.build_design_matrices" title="patsy.build_design_matrices"><code class="xref py py-func docutils literal"><span class="pre">build_design_matrices()</span></code></a> along with the new
data.</p>
</div>
<div class="section" id="example">
<h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<p>Here’s a simplified class for doing ordinary least-squares regression,
demonstrating the above techniques:</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">This code has not been validated for numerical
correctness.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">patsy</span> <span class="kn">import</span> <span class="n">dmatrices</span><span class="p">,</span> <span class="n">build_design_matrices</span>
<span class="k">class</span> <span class="nc">LM</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""An example ordinary least squares linear model class, analogous to R's</span>
<span class="sd"> lm() function. Don't use this in real life, it isn't properly tested."""</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">formula_like</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="p">{}):</span>
<span class="n">y</span><span class="p">,</span> <span class="n">x</span> <span class="o">=</span> <span class="n">dmatrices</span><span class="p">(</span><span class="n">formula_like</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">nobs</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">betas</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rss</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">lstsq</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_y_design_info</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">design_info</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_x_design_info</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">design_info</span>
<span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">summary</span> <span class="o">=</span> <span class="p">(</span><span class="s">"Ordinary least-squares regression</span><span class="se">\n</span><span class="s">"</span>
<span class="s">" Model: </span><span class="si">%s</span><span class="s"> ~ </span><span class="si">%s</span><span class="se">\n</span><span class="s">"</span>
<span class="s">" Regression (beta) coefficients:</span><span class="se">\n</span><span class="s">"</span>
<span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_y_design_info</span><span class="o">.</span><span class="n">describe</span><span class="p">(),</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_x_design_info</span><span class="o">.</span><span class="n">describe</span><span class="p">()))</span>
<span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_x_design_info</span><span class="o">.</span><span class="n">column_names</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">betas</span><span class="p">):</span>
<span class="n">summary</span> <span class="o">+=</span> <span class="s">" </span><span class="si">%s</span><span class="s">: </span><span class="si">%0.3g</span><span class="se">\n</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">return</span> <span class="n">summary</span>
<span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_data</span><span class="p">):</span>
<span class="p">(</span><span class="n">new_x</span><span class="p">,)</span> <span class="o">=</span> <span class="n">build_design_matrices</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">_x_design_info</span><span class="p">],</span>
<span class="n">new_data</span><span class="p">)</span>
<span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">new_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">betas</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">loglik</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_data</span><span class="p">):</span>
<span class="p">(</span><span class="n">new_y</span><span class="p">,</span> <span class="n">new_x</span><span class="p">)</span> <span class="o">=</span> <span class="n">build_design_matrices</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">_y_design_info</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_x_design_info</span><span class="p">],</span>
<span class="n">new_data</span><span class="p">)</span>
<span class="n">new_pred</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">new_x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">betas</span><span class="p">)</span>
<span class="n">sigma2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rss</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">nobs</span>
<span class="c"># It'd be more elegant to use scipy.stats.norm.logpdf here, but adding</span>
<span class="c"># a dependency on scipy makes the docs build more complicated:</span>
<span class="n">Z</span> <span class="o">=</span> <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">sigma2</span><span class="p">)</span>
<span class="k">return</span> <span class="n">Z</span> <span class="o">+</span> <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">(</span><span class="n">new_y</span> <span class="o">-</span> <span class="n">new_x</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="o">/</span><span class="n">sigma2</span>
</pre></div>
</div>
<p>And here’s how it can be used:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="kn">from</span> <span class="nn">patsy</span> <span class="kn">import</span> <span class="n">demo_data</span>
<span class="gp">In [2]: </span><span class="n">data</span> <span class="o">=</span> <span class="n">demo_data</span><span class="p">(</span><span class="s">"x"</span><span class="p">,</span> <span class="s">"y"</span><span class="p">,</span> <span class="s">"a"</span><span class="p">)</span>
<span class="go"># Old and boring approach (but it still works):</span>
<span class="gp">In [3]: </span><span class="n">X</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">column_stack</span><span class="p">(([</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s">"y"</span><span class="p">]),</span> <span class="n">data</span><span class="p">[</span><span class="s">"x"</span><span class="p">]))</span>
<span class="gp">In [4]: </span><span class="n">LM</span><span class="p">((</span><span class="n">data</span><span class="p">[</span><span class="s">"y"</span><span class="p">],</span> <span class="n">X</span><span class="p">))</span>
<span class="gh">Out[4]: </span><span class="go"></span>
<span class="go">Ordinary least-squares regression</span>
<span class="go"> Model: y0 ~ x0 + x1</span>
<span class="go"> Regression (beta) coefficients:</span>
<span class="go"> x0: 0.677</span>
<span class="go"> x1: -0.217</span>
<span class="go"># Fancy new way:</span>
<span class="gp">In [5]: </span><span class="n">m</span> <span class="o">=</span> <span class="n">LM</span><span class="p">(</span><span class="s">"y ~ x"</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="gp">In [6]: </span><span class="n">m</span>
<span class="gh">Out[6]: </span><span class="go"></span>
<span class="go">Ordinary least-squares regression</span>
<span class="go"> Model: y ~ 1 + x</span>
<span class="go"> Regression (beta) coefficients:</span>
<span class="go"> Intercept: 0.677</span>
<span class="go"> x: -0.217</span>
<span class="gp">In [7]: </span><span class="n">m</span><span class="o">.</span><span class="n">predict</span><span class="p">({</span><span class="s">"x"</span><span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">]})</span>
<span class="gh">Out[7]: </span><span class="go"></span>
<span class="go">array([[-1.48944498],</span>
<span class="go"> [-3.65620297],</span>
<span class="go"> [-5.82296096]])</span>
<span class="gp">In [8]: </span><span class="n">m</span><span class="o">.</span><span class="n">loglik</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gh">Out[8]: </span><span class="go"></span>
<span class="go">array([[ -0.28884193, -1.46289596],</span>
<span class="go"> [ -2.64235743, -0.8254485 ],</span>
<span class="go"> [ -2.44930737, -2.36666465],</span>
<span class="go"> [ -0.90233651, -6.24317017],</span>
<span class="go"> [ -1.58762894, -5.56817766],</span>
<span class="go"> [ -0.65148056, -10.80114045]])</span>
<span class="gp">In [9]: </span><span class="n">m</span><span class="o">.</span><span class="n">loglik</span><span class="p">({</span><span class="s">"x"</span><span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">],</span> <span class="s">"y"</span><span class="p">:</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]})</span>
<span class="gh">Out[9]: </span><span class="go"></span>
<span class="go">array([[ -7.39939265, -215.51261221],</span>
<span class="go"> [ -16.29311998, -861.19721649],</span>
<span class="go"> [ -28.74433824, -1937.33822362]])</span>
<span class="go"># Your users get support for categorical predictors for free:</span>
<span class="gp">In [10]: </span><span class="n">LM</span><span class="p">(</span><span class="s">"y ~ a"</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="gh">Out[10]: </span><span class="go"></span>
<span class="go">Ordinary least-squares regression</span>
<span class="go"> Model: y ~ 1 + a</span>
<span class="go"> Regression (beta) coefficients:</span>
<span class="go"> Intercept: 0.33</span>
<span class="go"> a[T.a2]: 0.241</span>
<span class="go"># And variable transformations too:</span>
<span class="gp">In [11]: </span><span class="n">LM</span><span class="p">(</span><span class="s">"y ~ np.log(x ** 2)"</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="gh">Out[11]: </span><span class="go"></span>
<span class="go">Ordinary least-squares regression</span>
<span class="go"> Model: y ~ 1 + np.log(x ** 2)</span>
<span class="go"> Regression (beta) coefficients:</span>
<span class="go"> Intercept: 0.399</span>
<span class="go"> np.log(x ** 2): 0.148</span>
</pre></div>
</div>
</div>
<div class="section" id="other-cool-tricks">
<h3>Other cool tricks<a class="headerlink" href="#other-cool-tricks" title="Permalink to this headline">¶</a></h3>
<p>If you want to compute ANOVAs, then check out
<a class="reference internal" href="API-reference.html#patsy.DesignInfo.term_name_slices" title="patsy.DesignInfo.term_name_slices"><code class="xref py py-attr docutils literal"><span class="pre">DesignInfo.term_name_slices</span></code></a>, <a class="reference internal" href="API-reference.html#patsy.DesignInfo.slice" title="patsy.DesignInfo.slice"><code class="xref py py-meth docutils literal"><span class="pre">DesignInfo.slice()</span></code></a>.</p>
<p>If you support linear hypothesis tests or otherwise allow your users
to specify linear constraints on model parameters, consider taking
advantage of <a class="reference internal" href="API-reference.html#patsy.DesignInfo.linear_constraint" title="patsy.DesignInfo.linear_constraint"><code class="xref py py-meth docutils literal"><span class="pre">DesignInfo.linear_constraint()</span></code></a>.</p>
</div>
</div>
<div class="section" id="extending-the-formula-syntax">
<h2>Extending the formula syntax<a class="headerlink" href="#extending-the-formula-syntax" title="Permalink to this headline">¶</a></h2>
<p>The above documentation assumes that you have a relatively simple
model that can be described by one or two matrices (plus whatever
other arguments you take). This covers many of the most popular
models, but it’s definitely not sufficient for every model out there.</p>
<p>Internally, Patsy is designed to be very flexible – for example,
it’s quite straightforward to add custom operators to the formula
parser, or otherwise extend the formula evaluation machinery. (Heck,
it only took an hour or two to repurpose it for a totally different
purpose, parsing linear constraints.) But extending Patsy in a
more fundamental way then this will require just a wee bit more complicated
API than just calling <a class="reference internal" href="API-reference.html#patsy.dmatrices" title="patsy.dmatrices"><code class="xref py py-func docutils literal"><span class="pre">dmatrices()</span></code></a>, and for this initial release,
we’ve been busy enough getting the basics working that we haven’t yet
taken the time to pin down a public extension API we can support.</p>
<p>So, if you want something fancier – please give us a nudge, it’s
entirely likely we can work something out.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Using Patsy in your library</a><ul>
<li><a class="reference internal" href="#using-the-high-level-interface">Using the high-level interface</a><ul>
<li><a class="reference internal" href="#working-with-metadata">Working with metadata</a></li>
<li><a class="reference internal" href="#predictions">Predictions</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#other-cool-tricks">Other cool tricks</a></li>
</ul>
</li>
<li><a class="reference internal" href="#extending-the-formula-syntax">Extending the formula syntax</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="expert-model-specification.html"
title="previous chapter">Model specification for experts and computers</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="R-comparison.html"
title="next chapter">Differences between R and Patsy formulas</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/library-developers.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="R-comparison.html" title="Differences between R and Patsy formulas"
>next</a> |</li>
<li class="right" >
<a href="expert-model-specification.html" title="Model specification for experts and computers"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">patsy 0.4.1 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2011-2015, Nathaniel J. Smith.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>
|