polars_online.ModelBank.fit¶
- ModelBank.fit(batches: LazyFrame | DataFrame | Iterable[DataFrame], *, closed_groups: str | PathLike[str] | None = None, chunk_size: int | None = None) None[source]¶
Learn every row of a query, a frame or an iterator of frames, and keep only the state.
Use it when the product of a run is the state, such as a bank you will save and serve, or coefficients you will read, and the predictions on the way are not needed. It goes on from the state the bank holds, as scikit-learn’s
partial_fitdoes, not as itsfit, which starts over: for a bank that starts with nothing learned, make a newModelBank. To keep the output too, usefit_predict_batches().Parameters
batchesThe stream: a
LazyFrame, aDataFrameor an iterator ofDataFramechunks, asfit_predict_batches()takes it.closed_groupsA path to which to write the closed groups’ rows, as
fit_predict_batches()takes it.None, the default, leaves them in the bank.chunk_sizeHow many rows of a query or a frame make one chunk, as
fit_predict_batches()takes it.None, the default, reads a query 100,000 rows at a time and takes a frame whole.
Raises
What
fit_predict_batches()raises, with two differences:OrderNotGuaranteedWarningis not issued when every spec reaches the same state in any row order (see Notes);a spec whose
embargois shorter than its formula target’s longest look-aheadwindow_sizeis not refused:fitlearns each of its rows once the window the row’s target reads has closed, whilefit_predict(), which would give predictions for them, refuses it.
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]) bank.fit(lf, chunk_size=100) # learn every row, keep no output bank.save("bank.state") print(bank.rows_fed)
400
See also
fit_predict_batches(), the same run with its output yielded.save(), which writes the state the run leaves.
Notes
fitisfit_predict_batches()with each chunk’s output dropped as it comes, so no output is held and no frame is assembled from the chunks. Over a long stream that saves the memory the output would take. A spec whose output on each row isweight_sumalone still writes gigabytes over a billion rows. A fit whose product is its coefficients has no use for its predictions. The statefitleaves is the statefit_predict_batches()leaves over the same rows, byte for byte. Each row is still predicted before it is learned, and the output columns are still built before they are dropped. Two calls over two days of rows leave the state one call over both would.A fit whose specs reach the same state in any row order is not warned about row order. Such a spec is an
ewridgeorrlswith no decay (lam=1.0orhalf_life=float("inf")) and none of the settings that read the row order, which are:a hard window, a session, a weight, an embargo or
label_clock;a drift reset, a
coefcadence or a formula target;gram_block_rows,restart_after_step_backorgroup_close;a
ridge_scaleother than"mean", or atarget_gapsother than"own_rows";conformal,long_half_life,session_shrinkoremit_averaged;any diagnostic but
emit_drift,emit_clocksandemit_error_inflation;any other setting not known to keep the state free of the row order.
Such a spec’s running sums are the same whatever order the rows are added in, to rounding. Measured over 200 rows, the coefficients differed by at most 6.7e-16 for
ewridgeand 1.4e-15 forrls. The warning stays forfit_predict_batches()over the same specs, whose predictions are out-of-sample and so move with the order: by up to 0.087 forewridgeon those rows. It stays for every model whose fit depends on the order it learns rows (sgd,pa,ftrl,quantileand a reweightinghuberdiffer materially with no decay at all), and for a model that chooses by its out-of-sample error, aslassochooses its penalty.