The online namespaces¶
Importing polars_online registers online on pl.LazyFrame and
pl.DataFrame. Each runs a model bank over the query’s or the frame’s rows
(fit_predict, predict), turns the bank’s output into one column per
field (unnest), and adds window operators’ values as columns
(with_windows). lf.online does it inside a query, which streams its
rows through when it runs, and df.online at once, on a frame in memory.
lf.online / df.online¶
A model bank inside a Polars query: lf.online.fit_predict(specs).
A query in, a query out. When the query runs, its rows go through a bank
that starts with nothing learned, one chunk at a time, so memory grows with
a chunk and the bank’s state, never with the length of the stream. The bank
is a source of the query, the kind of step Polars reads chunks of rows from,
and every step after it, such as a filter, a select, a join or writing
the result to a file, is Polars’ own. A filter after the bank never changes
what the bank learns from. To keep the bank from learning rows, give them
weight 0 in the spec’s weight column.
The query is pure: every run starts from the same state (the specs’, or
load_state’s, read when the query is built), so running it twice gives
the same frame. save_state writes the state the run ends in, after the
last row the query gave the bank, atomically. Because the query is pure,
that write gives the same bytes every time. Polars runs a query’s source
once per run, and twice, at the same time, when one query uses it twice (a
self-join, pl.concat, or pl.collect_all of two queries), and every
run ends in the same state.
df.online.fit_predict(specs) does the same for a frame in memory, as
ModelBank(specs).fit_predict(df) in one call. online.unnest(specs)
turns each spec’s output column into one column per field, with the
coef list as one named column per coefficient
(polars_online.spec.coef_fields()). online.with_windows(*exprs, **named)
adds exponentially weighted means, sums, rates, variances and quantiles,
looking back or ahead, as columns
(polars_online.stream.with_windows()), so a query can add them and
fit on them in one chain. Both namespaces are attached when the package is
imported, which no type checker can see. So fit_predict(),
predict(), unnest() and
polars_online.stream.with_windows() are the same calls with the query
or the frame as the first argument, visibly typed.
The |
|
The |