polars_online.ModelBank.last_row

ModelBank.last_row(spec: str | int | None = None, group: str | Iterable[str | None] | None = None) → DataFrame[source]

Return each model’s output on the last row it did not skip, one row per spec and group.

Use it to see how each model was doing at the end of a stream, from the bank or from one loaded from a state file, without keeping the output frame. For the coefficients alone, whichever row was last, use coef(), and for what each model was fed, summary().

Parameters

spec

The spec to read, by name or by position. The default, None, reads every spec.

group

The group to read: one group key, as groups() reports it, or a list of keys, with None in the list for the null group. The default, None, reads every group. A key the bank has never seen gives no rows.

Returns

A DataFrame with spec and group, then the row fit_predict() reported for that row, field for field, unnested: pred_<t>, resid_<t>, weight_sum, settled_frac, withheld_reason, the diagnostics’ fields, and coef where the row carried it. Rows come in spec order, then in groups()’ order. Specs with different fields are stacked into one frame, so a field a spec does not write is null on its rows. The row is the last one the model predicted, whether or not it learned from it: a row of weight 0 or with a null target replaces it, and a skipped row does not. A group whose every row so far was skipped is a row of nulls.

coef is on the row where the output carried it: by default each group’s last row in a chunk that was not skipped, and under coef_every or max_rows_between_coefs only a row on that schedule. So it can be null here where coef() has the fit.

Raises

KeyError

spec is a name the bank does not have.

IndexError

spec is a position the bank does not have.

TypeError

group is not a str, a list of them with None allowed, or None.

Examples

This code reads df from Example data:

spec = po.spec.ewridge(
    "ridge", targets=["y"], features=["x0", "x1"], half_life=100.0, group="stock_id"
)
bank = po.ModelBank([spec])
bank.fit(df)
last = bank.last_row()                              # one row per (spec, group)
for group, pred in last.select("group", "pred_y").iter_rows():
    print(f"{group}: {pred:.2f}")                   # each stock's last prediction
b0: 4.72
b1: -0.11
b2: 2.92
b3: 1.80

To compare saved banks, stack their last rows, each marked with its file:

bank.save("bank.state")                             # one saved bank; list more
files = ["bank.state"]
table = pl.concat(
    [po.ModelBank.load(f).last_row().with_columns(file=pl.lit(f)) for f in files],
    how="diagonal_relaxed",
)

See also

  • coef(), the coefficients of each fit, one row per coefficient.

  • summary(), what each model was fed.

  • fit_predict(), whose output fields this row repeats.

Notes

The row travels with the state, so a bank loaded from a file gives the row its saved bank would. predict() does not move it, and a chunk that ends in skipped rows leaves the row before them.