rheon.polars.eval.unpack¶
- rheon.polars.eval.unpack(df: DataFrame, spec: str | dict, *, targets: Sequence[str] | None = None) DataFrame[source]¶
- rheon.polars.eval.unpack(df: LazyFrame, spec: str | dict, *, targets: Sequence[str] | None = None) LazyFrame
Turn a spec’s output into long form, one row per input row and slot.
Use it to score or plot predictions with your own Polars code: each row holds one prediction beside the value of the target it predicts. For the standard scores, use
metrics(), which reads this long form itself.Parameters
dfA frame a bank returned: the input’s columns and the spec’s output column. Or a query of one, a
LazyFrame, which gives a query of the long form back (see Returns).specThe spec’s name, which is its output column’s, or the spec dict itself, which names the column too. Pass the dict to read each slot’s target as the spec reads it, through
rheon.spec.output_index()and the spec’stargets. A plain target is read from its column, and a target renamed withrheon.target()’sname=from the column it renames. A formula target is read from a column of its own name, whichrheon.polars.stream.with_windows()makes, since the bank computes the formula as it learns and does not write it out. Given the name alone, a slot is read against the column named after it, the longest name that matches. That rule can attribute a slot to the wrong target when a column of the frame is named as a target followed by a slot’s__or@suffix. A renamed target (rp.target("p", name="price")writespred_price) names no column. So only the dict can say what either read.targetsThe columns the name-based rule may read as targets; every column of
dfby default. A bare string is one name, asgrouptakes one. Not read whenspecis the dict.
Returns
A
DataFrameofdf’s columns that are not records, one row for each row ofdfand slot, with four columns added:slotThe name of the field the prediction is in, such as
pred_y__r0.5@h500.targetThe target the slot predicts: the name its output fields carry, which for a plain target is its column.
pred,yThe prediction and the target’s value on the row.
An input column named like one of these four (
RESERVED) is dropped, so a target column calledydoes not collide with theyadded.Given a
LazyFrame, aLazyFrameof the same rows, which runs only when its result is asked for, as Polars’ own calls on a query return a query. Its rows come slot by slot, as the frame’s do, so it readsdfonce for each slot: a query that runs a bank runs the bank once per slot. Write the bank’s output to a file first, withsink_parquet, and unpack a scan of the file. Written to a file withsink_parquet, the long form of a 20-slot output held 123 MiB at a million rows, 133 MiB at four million and 172 MiB at sixteen million undermetrics()’ bounded settings, and 656, 1,137 and 1,469 MiB under Polars’ defaults, which read the scan ahead of the writing: Cut what Polars reads ahead bounds that. Those figures hold while the disk keeps up: Polars’ writer queues what a slower disk has not taken yet, and the queue grows with the length of the output. Reading the long form in batches, ascollect_batchesdoes, holds the query back to the reader instead.Raises
TypeErrordfis neither aDataFramenor aLazyFrame; or the spec’s column is not a record, or holds nopred_*field, as anew_cov,kmeans,micro,ew_classorseqtestmodel writes; orspecis neither a name nor a spec dict.OutputColumnNotFoundErrordfhas no column of the spec’s. It is both Polars’ColumnNotFoundErrorand aKeyError.ColumnNotFoundErrorGiven the spec dict, a slot’s target column is not in
df, such as a formula target’s column not yet made. It is both Polars’ColumnNotFoundErrorand aValueError.ValueErrorGiven the name alone, no column of
df(or oftargets) is named after a slot; or, given the dict, whateverrheon.spec.output_index()raises for it.
Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) out = rp.ModelBank([spec]).fit_predict(df) long = rp.eval.unpack(out, spec) # one row per row of out and slot slot, target, pred, y = long.select("slot", "target", "pred", "y").row(399) print(f"{slot} predicts {target}: {pred:.3f} on the last row, where y is {y:.3f}")
pred_y predicts y: 1.768 on the last row, where y is 1.479
See also
metrics(), the scores of this long form per slot, target and group.rheon.spec.output_index(), each output field’s slot, target and grid point, read from the spec.