polars_online.ModelBank.fit_predict_batches

ModelBank.fit_predict_batches(batches: LazyFrame | DataFrame | Iterable[DataFrame], *, closed_groups: str | PathLike[str] | None = None, chunk_size: int | None = None) → Iterator[DataFrame][source]

Learn a stream one chunk at a time, and yield each chunk with each spec’s output.

Use it for a stream too large to hold in memory, when you want the output one chunk at a time, as a generator. For a frame in memory, use fit_predict(). To keep only the state, use fit(), and to run the bank as a step of a query, use lf.online.fit_predict.

Parameters

batches

The stream: a LazyFrame, a DataFrame or an iterator of DataFrame chunks. A query is run chunk_size rows at a time, and each chunk is learned when the generator reaches it. A frame is one chunk, or slices of chunk_size rows when chunk_size is given. An iterator’s frames are learned as they come.

closed_groups

A path to which to write the closed groups’ rows, in the format its extension names (see Notes). It takes a spec that closes groups, under group_close. None, the default, leaves the closed groups in the bank, for closed_groups() to read.

chunk_size

How many rows of a query or a frame make one chunk. None, the default, reads a query 100,000 rows at a time and takes a frame whole. It does not cut an iterator’s frames.

Returns

A generator of DataFrame chunks, each the chunk’s rows with one output column added for each spec, as fit_predict() returns them.

Raises

When called, before a row is read:

WrongTypeError

chunk_size is not a whole number, such as 2.5. It is both a TypeError and a ValueError.

TypeError

closed_groups is neither a string nor a path.

ValueError

chunk_size is below 1, or closed_groups names no format by its extension, or names a file beside specs none of which sets group_close, or names a CSV file while the closed rows hold a nested column that CSV cannot hold.

FileNotFoundError

closed_groups names a file in a folder that does not exist.

OrderNotGuaranteedWarning

A warning, not an error: issued when the query holds a step that does not keep row order, naming the step (see Notes).

As the generator runs:

TypeError

An iterator yields something other than a DataFrame.

Any error fit_predict() raises

For the chunk where it happens. The chunks before it have been learned.

ConsumedSourceWarning

A warning, not an error: the query gave no rows, and it reads a source written in Python, which may give its rows only once.

Examples

This code reads lf from Example data:

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
bank = po.ModelBank([spec])
for out in bank.fit_predict_batches(lf, chunk_size=100):
    print(out.height, out["ridge"].struct.field("pred_y").count())   # each chunk
100 96
100 100
100 100
100 100

See also

  • fit_predict(), the same over one frame.

  • fit(), which learns the same rows and keeps no output.

  • closed_groups(), which reads the closed groups between chunks.

  • lf.online.fit_predict, the bank as a step of a query.

Notes

Memory holds the state and one chunk, however long the stream.

A model learns rows in their order, so the order in which Polars delivers a query’s rows is part of the result. A join, group_by or unique that does not keep order may deliver the rows in another order than the query’s whole result has, and in another order from one run to the next. Give a join maintain_order="left", a group_by maintain_order=True, and sort after a unique, or sort before the bank.

Under closed_groups, the bank’s closed groups are read out after every chunk, so none waits in the bank when a chunk is handed on, and the rows read out are written to the file when the chunks stop. Without it, or a call to closed_groups() between chunks, every closed group waits in the bank, and save() writes them all. The file is written however the chunks stop, at their end, at a break or at an error, since a row read out of the bank is then in no other place. The file and the bank together always hold every group that closed. A CSV file holds each list column, such as coef, as JSON text, the bytes the command line writes. Read it back with Polars’ str.json_decode.