File: regression.rst

package info (click to toggle)
python-altair 5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,952 kB
  • sloc: python: 25,649; sh: 14; makefile: 5
file content (53 lines) | stat: -rw-r--r-- 1,605 bytes parent folder | download
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
.. currentmodule:: altair

.. _user-guide-regression-transform:

Regression
~~~~~~~~~~

The regression transform fits two-dimensional regression models to smooth and
predict data. This transform can fit multiple models for input data (one per group)
and generates new data objects that represent points for summary trend lines.
Alternatively, this transform can be used to generate a set of objects containing
regression model parameters, one per group.

This transform supports parametric models for the following functional forms:

- linear (``linear``): *y = a + b * x*
- logarithmic (``log``): *y = a + b * log(x)*
- exponential (``exp``): * y = a * e^(b * x)*
- power (``pow``): *y = a * x^b*
- quadratic (``quad``): *y = a + b * x + c * x^2*
- polynomial (``poly``): *y = a + b * x + … + k * x^(order)*

All models are fit using ordinary least squares.
For non-parametric locally weighted regression, see the
:ref:`user-guide-loess-transform`.

Here is an example of a simple linear regression plotted on top of data:

.. altair-plot::

   import altair as alt
   import pandas as pd
   import numpy as np

   np.random.seed(42)
   x = np.linspace(0, 10)
   y = x - 5 + np.random.randn(len(x))

   df = pd.DataFrame({'x': x, 'y': y})

   chart = alt.Chart(df).mark_point().encode(
       x='x',
       y='y'
   )

   chart + chart.transform_regression('x', 'y').mark_line()

Transform Options
^^^^^^^^^^^^^^^^^
The :meth:`~Chart.transform_regression` method is built on the :class:`~RegressionTransform`
class, which has the following options:

.. altair-object-table:: altair.RegressionTransform