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, usefit(), and to run the bank as a step of a query, uself.online.fit_predict.Parameters
batchesThe stream: a
LazyFrame, aDataFrameor an iterator ofDataFramechunks. A query is runchunk_sizerows at a time, and each chunk is learned when the generator reaches it. A frame is one chunk, or slices ofchunk_sizerows whenchunk_sizeis given. An iterator’s frames are learned as they come.closed_groupsA 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, forclosed_groups()to read.chunk_sizeHow 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
DataFramechunks, each the chunk’s rows with one output column added for each spec, asfit_predict()returns them.Raises
When called, before a row is read:
WrongTypeErrorchunk_sizeis not a whole number, such as2.5. It is both aTypeErrorand aValueError.TypeErrorclosed_groupsis neither a string nor a path.ValueErrorchunk_sizeis below 1, orclosed_groupsnames no format by its extension, or names a file beside specs none of which setsgroup_close, or names a CSV file while the closed rows hold a nested column that CSV cannot hold.FileNotFoundErrorclosed_groupsnames a file in a folder that does not exist.OrderNotGuaranteedWarningA 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:
TypeErrorAn 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.
ConsumedSourceWarningA 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
lffrom 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_byoruniquethat 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 joinmaintain_order="left", agroup_bymaintain_order=True, and sort after aunique, 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 toclosed_groups()between chunks, every closed group waits in the bank, andsave()writes them all. The file is written however the chunks stop, at their end, at abreakor 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 ascoef, as JSON text, the bytes the command line writes. Read it back with Polars’str.json_decode.