rheon.polars.ModelBank.skip_learned¶
- ModelBank.skip_learned(frame: DataFrame, *, clock_runs_on: bool = False) DataFrame[source]¶
- ModelBank.skip_learned(frame: LazyFrame, *, clock_runs_on: bool = False) LazyFrame
The rows of a frame or query that the bank has not learned.
Use it to resume a saved bank on input that overlaps what the bank learned before it was saved, such as a rerun of the day or a file that overlaps the last one. A loaded bank goes on from the next row, so the overlap steps each group’s clock back, which
fit_predict()refuses unlessrestart_after_step_backreads it as a new start. Without a clock in any spec, pass the bank only the rows after the last one it learned.It finds the rows by each group’s last clock, so it is right only where, within a group, the clock never goes back. A spec with
sessionorrestart_after_step_backlets a group’s clock start again lower, and on such a stream the last clock is not a place in it: the rule would drop the next session’s unlearned rows and keep learned ones, and the bank would take them without a word. Nothing in the frame or the state tells that stream from one whose clock runs on across sessions, such as a timestamp with the day as the session, so under those settings the call refuses unlessclock_runs_on=Truesays the clock runs on.Parameters
frameThe input, a
DataFrameor aLazyFrame, holding each spec’s clock column andgroupcolumn.clock_runs_onTruedeclares that, within each group, the clock never goes back, across sessions and step backs included. Needed when a spec that reads a clock setssessionorrestart_after_step_back; accepted, and changing nothing, otherwise. Where the clock does start again, do not set it: filter the input before the bank on a column that orders the whole stream, such as a date or a row number, instead.
Returns
frame’s rows that the bank has not learned, of the same type, in the same order: aLazyFramestays lazy. A row is kept when, in every spec that reads a clock, its clock is after its group’s last clock, its group is one that spec has not seen, or its clock is null. A null clock is left forfit_predict()to judge. A row at its group’s last clock counts as learned.Raises
ValueErrorNo spec reads a clock, so there is no position on a clock to resume from; or a spec that reads a clock sets
sessionorrestart_after_step_backandclock_runs_onis notTrue.WrongTypeErrorA clock column is not a number, a
Date, aDatetimeor aDuration, or a clock is temporal in the frame and was numeric in the bank, or the other way round. It is both aTypeErrorand aValueError.ColumnNotFoundErrorA spec’s clock or group column is not in the frame. It is both a
ValueErrorand Polars’ColumnNotFoundError.TypeErrorframeis neither aDataFramenor aLazyFrame, such as a dict, orclock_runs_onis not a bool.
Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge( "m", targets=["y"], features=["x0"], clock="t", half_life=50.0, gap_cap=10.0 ) saved = rp.ModelBank([spec]) saved.fit_predict(df.head(300)) # the stream as far as the save saved.save("bank.state") bank = rp.ModelBank.load("bank.state") rerun = df # the whole day again: its first 300 rows overlap out = bank.fit_predict(bank.skip_learned(rerun)) print(f"{out.height} rows learned, from t = {out['t'][0]}")
100 rows learned, from t = 300.0
See also
load(), which loads the saved bank.
Notes
The comparison is exact. A temporal clock is compared in the integer nanoseconds the bank keeps, whatever its unit or time zone, and an integer clock in integers. Since a row at its group’s last clock counts as learned, a stream that repeats a clock value and was saved between two rows with that value loses the later ones.