polars_online.ModelBank.fit_predict¶
- ModelBank.fit_predict(df: DataFrame) DataFrame[source]¶
Learn a frame’s rows, and return them with each spec’s output.
Use it for rows held in memory as a frame, when you want the output back as one frame. For a query or a stream too large to hold, use
lf.online.fit_predictorfit_predict_batches(). To learn without keeping the output, usefit(), and to predict without learning,predict().Parameters
dfThe next rows of the stream, as one chunk: a
DataFrameholding every column the bank’s specs read. Those are the specs’ targets and features, the clock, session, weight, group andlabel_clockcolumns they name, and the columns a formula target’s expression reads. In each group of a spec with a clock, the rows continue the group’s clock within each session. A step back is refused, unlessrestart_after_step_backis set below it, where the model resets.
Returns
dfwith one output column added for each spec, named after the spec, whose value in each row is a record of named fields. For a regression they arepred_<t>,resid_<t>,weight_sum,settled_frac,withheld_reasonandcoef, withsupport_coefwhere the model reports it,lasso’spenalty_selected_<t>, and the fields of the diagnostics switched on. Under a grid of half-lives every field read from one instance carries its suffix, such asweight_sum@h100(an instance is the part of the model that fits one half-life). The fields that choose across instances (emit_selected,emit_averaged) and thoseemit_clockswrites carry none. Under a grid ofridgevalues, feature sets or penalties, each slot’s fields, itspred, itsresidand its diagnostics, carry the slot’s suffix, such aspred_y__r0.1.output_fieldslists a bank’s fields,polars_online.specdescribes them under What a spec writes, and docs/OUTPUTS.md gives every model’s. Each prediction is out-of-sample: the model makes it from what it learned before the row, and only then learns the row.coef,support_coefand the standard errors are the fit after the row.Raises
TypeErrordfis not aDataFrame, such as aLazyFrame.ColumnNotFoundErrorA column a spec reads is not in
df. It is both aValueErrorand Polars’ColumnNotFoundError.WrongTypeErrorA column of a type its role cannot take. It is both a
TypeErrorand aValueError. When:a column of
dfisUInt128, which polars-online cannot read;a target column (other than
ew_class’s), a feature or a weight column is neither numeric nor Boolean;a clock is not a number, a Boolean, a
Datetime, aDateor aDuration(aTimestarts again at midnight), or is anInt128;a temporal column is read as a feature, target or weight, or by a formula target, other than as the clock;
a
label_clockcolumn’s type does not match the clock’s, a temporal one for a temporal clock and a numeric one for a numeric clock;a group or session column, or an
ew_classtarget, has a type with no text form, such as a nested one;under
group_close="monotone", the group column is not an integer, String or Categorical type.
ValueErrorWhen:
a clock is a
UInt64past the largestInt64;between chunks, an integer clock column changes its width or becomes a float, a float clock becomes an integer, or a group or session column changes between integer, float, text and Boolean keys;
a spec gives its clock the other kind of clock parameter: a plain number to a temporal clock, or a duration to a numeric one (Clock units in
polars_online.spec), or agap_caporrestart_after_step_backfiner than the clock’s smallest step;the clock has a null or non-finite value, or a step between two of its values that is not a finite number;
a weight is negative (a null weight skips the row);
a spec is named like an input column, which its output column would replace;
a group’s clock steps back within a session, with
restart_after_step_backnot set, the default, or by no more than it. A chunk that overlaps what a loaded state has learned is refused the same way, andskip_learned()drops the overlap;under
group_close="monotone", a group key is null or below the key before it;a
label_clocktime is before its row’s clock, or a group’s rows waiting for theirlabel_clocktimes would passmax_held_rowsunderheld_overflow="refuse", the default;a formula target’s
embargois shorter than its longest look-aheadwindow_size;a chunk would take a hard window’s snapshots past a
window_budgetthat refuses;a model refuses a value of its own, such as a
strict_binarytarget that is not 0 or 1, as its builder’s docstring says;the bank refused an earlier chunk after learning part of it (see Notes);
POLARS_ONLINE_MAX_THREADSis not a whole number, or is above 1024 (or the machine’s core count, where that is larger), on a bank withthreads=None, at the first call that runs on the process’s threads.
RuntimeErrorThe bank is in use on another thread (
ModelBank).ReadinessWarningA warning, not an error: issued when a finding that casts doubt on what a model reports first holds, such as a gate still unmet once the model has learned long enough to judge. That class’s docstring lists each finding and how often it is issued.
Examples
This code reads
dffrom Example data:spec = po.spec.ewridge( "ridge", targets=["y"], features=["x0", "x1"], half_life=100.0, ridge=[1e-6, 0.1] ) bank = po.ModelBank([spec]) out = bank.fit_predict(df) # df, with the column "ridge" added pred = out["ridge"].struct.field("pred_y__r0.1") # the slot with ridge 0.1 first = pred.is_not_null().arg_true()[0] # the first row after its warm-up print(f"{pred.count()} of {out.height} rows predicted, the first at row {first}")
396 of 400 rows predicted, the first at row 4
See also
lf.online.fit_predict, the same over a query, run as the query runs.fit_predict_batches(), the same over an iterator of chunks.fit(), which learns and keeps no output.predict(), which predicts and learns nothing.
Notes
Chunk boundaries never change the numbers: a stream fed as one chunk or a thousand gives the same predictions to the bit. By default they move the rows
coefis reported on, each group’s last row in the chunk; undercoef_everyormax_rows_between_coefsnot even those.A refused chunk leaves the bank as it was, so the corrected chunk can be fed. The bank checks every group’s clock steps in every spec over the whole chunk, and plays the chunk’s clock through each hard window’s snapshot schedule, before it learns a row. Two overruns of a
window_budgetare found only as the rows are learned: one underdrift_action="reset", whose resets the check cannot foresee, and one where a snapshot grows within the chunk. Then the chunk is refused after some of it has been learned, and the bank refuses every later call that learns, predicts or saves: load its last saved state withload()and feed it the rows since that save.