LOESS Transform#

The LOESS transform (LOcally Estimated Scatterplot Smoothing) uses a locally-estimated regression to produce a trend line. LOESS performs a sequence of local weighted regressions over a sliding window of nearest-neighbor points. For standard parametric regression options, see the Regression Transform.

Here is an example of using LOESS to smooth samples from a Gaussian random walk:

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

np.random.seed(42)

df = pd.DataFrame({
    'x': range(100),
    'y': np.random.randn(100).cumsum()
})

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

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

Transform Options#

The transform_loess() method is built on the LoessTransform class, which has the following options:

Property

Type

Description

as

array(FieldName)

The output field names for the smoothed points generated by the loess transform.

Default value: The field names of the input x and y values.

bandwidth

number

A bandwidth parameter in the range [0, 1] that determines the amount of smoothing.

Default value: 0.3

groupby

array(FieldName)

The data fields to group by. If not specified, a single group containing all data objects will be used.

loess

FieldName

The data field of the dependent variable to smooth.

on

FieldName

The data field of the independent variable to use a predictor.