polars_online.ModelBank.predict

ModelBank.predict(df: DataFrame) → DataFrame[source]

Predict a frame’s rows from the bank as it stands, learning nothing.

Use it to serve a bank that has learned: load() it once, call predict for each request, and feed the same rows to fit_predict() later, in order, once their targets arrive. The bank is left exactly as it was, so any number of threads may call predict at once, and row order does not matter. For a query, use lf.online.predict.

Parameters

df

The rows to predict, as a DataFrame. Each row is predicted as the next row of its group’s stream would be. df needs each spec’s feature, clock and group columns. A target column is optional: present, it gives resid and the standardized residual, and absent, they are null. The session column is optional, and is read for session_gap and group_close="session". A weight column is not read.

Returns

df with each spec’s output column added, with the fields fit_predict() gives, computed from the current state. Some fields and rows differ from what fit_predict would give:

the field or the row

what predict gives

weight_sum, penalty_selected_<t>, sigma, the residual quantiles, the autocorrelation and the metrics

the values the bank holds, unchanged by the rows

coef

filled on each group’s last row that is not skipped, since the same coefficients predict every row. coef_every and max_rows_between_coefs are not read

drift

never fires

a row of a group the bank has never seen, or a row whose features are null

null throughout, as a skipped row is in fit_predict

under group_close="session", a row from a session other than the group’s current one

the first row of a new stream, null with weight_sum 0, since fit_predict would start the stream again there

a row on which session_gap="reset" would reset the model

the same

a row before the group’s last learned clock

predicted from the state as it stands, whatever restart_after_step_back says: predicting learns nothing, so it neither refuses the row nor resets

Raises

What fit_predict() raises for a frame’s columns and their types, such as a missing column or a bad clock value, with these differences:

  • it does not refuse what only learning would: a step back, a negative weight, a window_budget overrun or held rows past max_held_rows;

  • a missing target column is not an error;

  • RuntimeError comes only when a call that learns is in flight on another thread, since predict calls may run at once.

Examples

This code reads df and today from Example data:

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
bank = po.ModelBank([spec])
bank.fit_predict(df)                        # learn df's rows
served = bank.predict(today.drop("y"))      # the next rows, their targets not yet known
pred = served["ridge"].struct.field("pred_y")
print(f"{pred.count()} of {served.height} rows predicted, {bank.rows_fed} rows learned")
400 of 400 rows predicted, 400 rows learned

See also

  • fit_predict(), which predicts each row and then learns it.

  • predict_arrow(), the same with the output as Arrow.

  • lf.online.predict, the same over a query.

Notes

A trend model (holt) extrapolates over the clock distance from the row it last learned, capped by gap_cap. A kalman with revert_half_life shrinks its coefficients over that distance exactly as the next fit_predict row would. The other models that predict from coefficients use their current coefficients whatever the clock says.

Under an embargo, predict releases nothing. A held row whose delay has passed by the predicted row’s clock stays held, and the next fit_predict learns it. So every row is predicted from the model the last fit_predict left, its weight_sum and learned_clock included.