rheon.polars.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, callpredictfor each request, and feed the same rows tofit_predict()later, in order, once their targets arrive. The bank is left exactly as it was, so any number of threads may callpredictat once, and row order does not matter. For a query, uself.rheon.predict.Parameters
dfThe rows to predict, as a
DataFrame. Each row is predicted as the next row of its group’s stream would be.dfneeds each spec’s feature, clock andgroupcolumns. A target column is optional: present, it givesresidand the standardized residual, and absent, they are null. The session column is optional, and is read forsession_gapandgroup_close="session". A weight column is not read.
Returns
dfwith each spec’s output column added, with the fieldsfit_predict()gives, computed from the current state. Some fields and rows differ from whatfit_predictwould give:the field or the row
what
predictgivesweight_sum,penalty_selected_<t>,sigma, the residual quantiles, the autocorrelation and the metricsthe values the bank holds, unchanged by the rows
coeffilled on each group’s last row that is not skipped, since the same coefficients predict every row.
coef_everyandmax_rows_between_coefsare not readdriftnever 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_predictunder
group_close="session", a row from a session other than the group’s current onethe first row of a new stream, null with
weight_sum0, sincefit_predictwould start the stream again therea row on which
session_gap="reset"would reset the modelthe same
a row before the group’s last learned clock
predicted from the state as it stands, whatever
restart_after_step_backsays: predicting learns nothing, so it neither refuses the row nor resetsRaises
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_budgetoverrun or held rows pastmax_held_rows;a missing target column is not an error;
RuntimeErrorcomes only when a call that learns is in flight on another thread, sincepredictcalls may run at once.
Examples
This code reads
dfandtodayfrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = rp.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.rheon.predict, the same over a query.
Notes
A trend model (
holt) extrapolates over the clock distance from the row it last learned, capped bygap_cap. Akalmanwithrevert_half_lifeshrinks its coefficients over that distance exactly as the nextfit_predictrow would. The other models that predict from coefficients use their current coefficients whatever the clock says.Under an
embargo,predictreleases nothing. A held row whose delay has passed by the predicted row’s clock stays held, and the nextfit_predictlearns it. So every row is predicted from the model the lastfit_predictleft, itsweight_sumandlearned_clockincluded.