rheon.polars.eval.sums

rheon.polars.eval.sums(df: DataFrame | LazyFrame, spec: str | dict, *, group: str | Iterable[str] | None = None, targets: Sequence[str] | None = None, weight: str | None = None, binary: bool | None = None) → DataFrame[source]

Reduce a bank’s output, or one chunk of it, to the sums its scores are read from.

Use it to keep scores that can be added up: reduce each part of a stream to ten numbers per slot, target and group, add the parts with merge_sums(), and read metrics()’ scores with from_sums(). Given a query, it reduces all of the query’s rows, one chunk at a time. For the scores themselves, metrics() gives them in one call, over a frame or a query.

Parameters

df

A frame a bank returned, or one chunk of it, holding the spec’s output column. Or a query of one, a LazyFrame, which this runs and reads one chunk at a time, as metrics() reads one.

spec

The spec’s name, or the spec dict itself, as unpack() reads it.

group, targets

metrics()’ own.

weight

A column to weight the rows by; None, the default, counts every row 1, and then w equals n. A row whose weight is null, NaN, infinite, past the input bound of 1e100, or negative is dropped: the bank skips the first four and refuses a negative one. A row of weight 0 is kept, counted in n and not in w, so from_sums()’ min_samples counts it: 30 rows of which 28 weigh 0 pass min_samples=30 and are scored on two. Filter out the rows that are not to count before this call, rather than weighting them 0, where min_samples is to count the rows scored.

binary

metrics()’ own: whether hits and signed count a classifier’s accuracy at 0.5 or the sign test. Keep the sums of the two readings apart (see Returns). A target other than 0 or 1 on a scored row is refused under it.

Returns

A DataFrame with one row per slot, target and group key, given a frame or a query alike, and these columns after the keys, SUM_FIELDS. A slot or key with no scored row has no row.

n, w

The rows, and the weight behind them.

mean_y, mean_pred

The weighted means of the target and the prediction.

m2_y, m2_pred, cov

The weighted second moments about those means, and the weighted cross-moment about them.

sse

The weighted sum of squared residuals.

hits, signed

For the hit rate: hits weighs the rows whose sign the prediction got right, and signed the rows with y != 0 and pred != 0, as metrics() counts them. Under binary, hits weighs the rows on the right side of 0.5, and signed every row. With the spec dict naming a Poisson sgd fit, which has no sign to hit, both are 0, and from_sums() gives the null the bank gives. merge_sums() adds hits and signed without knowing which reading made them, so add only sums made under one binary.

Raises

Whatever unpack() raises for df and spec, and ColumnNotFoundError, both Polars’ ColumnNotFoundError and a ValueError, for a group or weight column df does not have. What metrics() raises for group and binary, and ValueError for a weight column named like a column of unpack()’s long form (RESERVED). A query is refused as its result would be, with the same message, before it runs.

Examples

This code reads df from Example data:

spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0)
out = rp.ModelBank([spec]).fit_predict(df)
part = rp.eval.sums(out.head(200), spec)         # ten numbers per slot and target
rest = rp.eval.sums(out.tail(200), spec)
running = rp.eval.merge_sums(part, rest)         # the two halves, added
scores = rp.eval.from_sums(running)
whole = rp.eval.metrics(out, spec)
print(f"r2 from sums {scores['r2'][0]:.6f}, from the frame {whole['r2'][0]:.6f}")
r2 from sums 0.944014, from the frame 0.944014

See also

  • merge_sums(), which adds the sums of several chunks.

  • from_sums(), which reads the scores from them.

  • metrics(), the same scores in one call, over a frame or a query.

Notes

A run that compares fifty slots over a billion rows keeps ten numbers per key this way, instead of writing the rows out to score them later. Rows where the prediction or the target is null are dropped, as metrics() drops them: null, NaN, infinite or past the bank’s input bound.

The moments are kept about the mean, not as raw sums. The simpler form keeps sum(y) and sum(y**2) and subtracts, and loses the variance entirely when the mean is large against the spread. A target of variance 1 about 1e8 has var / E[y**2] of about 1e-16, and the subtraction has nothing left. Keeping the moments about the mean adds one term to merge_sums(), a multiply, and keeps the variance to rounding, as its Notes measure.

Given a query, the sums are those of the query’s chunks of 100,000 rows, each reduced as a frame is and added as merge_sums() adds them, so they hold what metrics()’ Notes measure, and Polars what it reads ahead of them, which Cut what Polars reads ahead bounds: 204, 208 and 212 MiB at one, four and sixteen million rows under its bounded settings, 493, 990 and 1,072 MiB under Polars’ defaults. While the query gives one chunk, they are the frame’s to the bit; over many, within the merge’s rounding, which metrics()’ Notes measure.