rheon.polars.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
specThe spec to read, by name or by position. The default,
None, reads every spec.groupThe group to read: one group key, as
groups()reports it, or a list of keys, withNonein the list for the null group. The default,None, reads every group. A key the bank has never seen gives no rows.
Returns
A
DataFramewithspecandgroup, then the rowfit_predict()reported for that row, field for field, unnested:pred_<t>,resid_<t>,weight_sum,settled_frac,withheld_reason, the diagnostics’ fields, andcoefwhere the row carried it. Rows come in spec order, then ingroups()’ 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.coefis on the row where the output carried it: by default each group’s last row in a chunk that was not skipped, and undercoef_everyormax_rows_between_coefsonly a row on that schedule. So it can be null here wherecoef()has the fit.Raises
KeyErrorspecis a name the bank does not have.IndexErrorspecis a position the bank does not have.TypeErrorgroupis not a str, a list of them withNoneallowed, orNone.
Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge( "ridge", targets=["y"], features=["x0", "x1"], half_life=100.0, group="stock_id" ) bank = rp.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( [rp.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.