polars_online.eval.unpack

polars_online.eval.unpack(df: DataFrame, spec: str | dict, *, targets: Sequence[str] | None = None) → DataFrame[source]
polars_online.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

df

A 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).

spec

The 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 polars_online.spec.output_index() and the spec’s targets. A plain target is read from its column, and a target renamed with polars_online.target()’s name= from the column it renames. A formula target is read from a column of its own name, which polars_online.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 (po.target("p", name="price") writes pred_price) names no column. So only the dict can say what either read.

targets

The columns the name-based rule may read as targets; every column of df by default. A bare string is one name, as group takes one. Not read when spec is the dict.

Returns

A DataFrame of df’s columns that are not records, one row for each row of df and slot, with four columns added:

slot

The name of the field the prediction is in, such as pred_y__r0.5@h500.

target

The target the slot predicts: the name its output fields carry, which for a plain target is its column.

pred, y

The 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 called y does not collide with the y added.

Given a LazyFrame, a LazyFrame of 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 reads df once for each slot: a query that runs a bank runs the bank once per slot. Write the bank’s output to a file first, with sink_parquet, and unpack a scan of the file. Written to a file with sink_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 under metrics()’ 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, as collect_batches does, holds the query back to the reader instead.

Raises

TypeError

df is neither a DataFrame nor a LazyFrame; or the spec’s column is not a record, or holds no pred_* field, as an ew_cov, kmeans, micro, ew_class or seqtest model writes; or spec is neither a name nor a spec dict.

OutputColumnNotFoundError

df has no column of the spec’s. It is both Polars’ ColumnNotFoundError and a KeyError.

ColumnNotFoundError

Given 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’ ColumnNotFoundError and a ValueError.

ValueError

Given the name alone, no column of df (or of targets) is named after a slot; or, given the dict, whatever polars_online.spec.output_index() raises for it.

Examples

This code reads df from Example data:

spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
out = po.ModelBank([spec]).fit_predict(df)
long = po.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