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 readmetrics()’ scores withfrom_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
dfA 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, asmetrics()reads one.specThe spec’s name, or the spec dict itself, as
unpack()reads it.group,targetsmetrics()’ own.weightA column to weight the rows by;
None, the default, counts every row 1, and thenwequalsn. 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 innand not inw, sofrom_sums()’min_samplescounts it: 30 rows of which 28 weigh 0 passmin_samples=30and are scored on two. Filter out the rows that are not to count before this call, rather than weighting them 0, wheremin_samplesis to count the rows scored.binarymetrics()’ own: whetherhitsandsignedcount 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
DataFramewith one row perslot,targetandgroupkey, 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,wThe rows, and the weight behind them.
mean_y,mean_predThe weighted means of the target and the prediction.
m2_y,m2_pred,covThe weighted second moments about those means, and the weighted cross-moment about them.
sseThe weighted sum of squared residuals.
hits,signedFor the hit rate:
hitsweighs the rows whose sign the prediction got right, andsignedthe rows withy != 0andpred != 0, asmetrics()counts them. Underbinary,hitsweighs the rows on the right side of 0.5, andsignedevery row. With the spec dict naming a Poissonsgdfit, which has no sign to hit, both are 0, andfrom_sums()gives the null the bank gives.merge_sums()addshitsandsignedwithout knowing which reading made them, so add only sums made under onebinary.
Raises
Whatever
unpack()raises fordfandspec, andColumnNotFoundError, both Polars’ColumnNotFoundErrorand aValueError, for agrouporweightcolumndfdoes not have. Whatmetrics()raises forgroupandbinary, andValueErrorfor aweightcolumn named like a column ofunpack()’s long form (RESERVED). A query is refused as its result would be, with the same message, before it runs.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) 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)andsum(y**2)and subtracts, and loses the variance entirely when the mean is large against the spread. A target of variance 1 about 1e8 hasvar / E[y**2]of about 1e-16, and the subtraction has nothing left. Keeping the moments about the mean adds one term tomerge_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 whatmetrics()’ 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, whichmetrics()’ Notes measure.