rheon.polars.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_fit does, not as its fit, which starts over: for a bank that starts with nothing learned, make a new ModelBank. To keep the output too, use fit_predict_batches().

Parameters

batches

The stream: a LazyFrame, a DataFrame or an iterator of DataFrame chunks, as fit_predict_batches() takes it.

closed_groups

A path to which to write the closed groups’ rows, as fit_predict_batches() takes it. None, the default, leaves them in the bank.

chunk_size

How 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:

  • OrderNotGuaranteedWarning is not issued when every spec reaches the same state in any row order (see Notes);

  • a spec whose embargo is shorter than its formula target’s longest look-ahead window_size is not refused: fit learns each of its rows once the window the row’s target reads has closed, while fit_predict(), which would give predictions for them, refuses it.

Examples

This code reads lf from Example data:

spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
bank = rp.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

Notes

fit is fit_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 is weight_sum alone still writes gigabytes over a billion rows. A fit whose product is its coefficients has no use for its predictions. The state fit leaves is the state fit_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 ewridge or rls with no decay (lam=1.0 or half_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 coef cadence or a formula target;

  • gram_block_rows, restart_after_step_back or group_close;

  • a ridge_scale other than "mean", or a target_gaps other than "own_rows";

  • conformal, long_half_life, session_shrink or emit_averaged;

  • any diagnostic but emit_drift, emit_clocks and emit_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 ewridge and 1.4e-15 for rls. The warning stays for fit_predict_batches() over the same specs, whose predictions are out-of-sample and so move with the order: by up to 0.087 for ewridge on those rows. It stays for every model whose fit depends on the order it learns rows (sgd, pa, ftrl, quantile and a reweighting huber differ materially with no decay at all), and for a model that chooses by its out-of-sample error, as lasso chooses its penalty.