rheon.polars.eval.from_sums

rheon.polars.eval.from_sums(s: DataFrame, *, min_samples: int = 30, binary: bool = False) → DataFrame[source]

Read metrics()’ scores from the sums sums() made.

Use it last, after sums() has reduced each chunk of a stream and merge_sums() has added them, to get the scores metrics() would give over every row at once.

Parameters

s

A frame sums() or merge_sums() returned.

min_samples

The fewest rows a key is scored on, 30 by default. A key with fewer rows keeps its row, with n filled and the scores null, as metrics() keeps it. It counts rows, n, whatever they weigh: a row of weight 0 counts (see sums()’ weight).

binary

Say that the sums were made under sums()’ binary, and name the columns as metrics() names them under it: brier_skill for r2 and accuracy (at 0.5) for hit_rate. False by default. The sums do not record their reading, so this cannot read it from them: which reading hit_rate is was fixed when the sums were made, since this divides hits by signed.

Returns

A DataFrame of the keys and metrics()’ columns, with the same numbers: n, r2 (out-of-sample, against the target’s mean), ic (the correlation of the prediction with the target), hit_rate, mse and rmse. Sums made with sums()’ weight give each score weighted, mse as sse / w. r2 is null where the target never varied, and ic where either the target or the prediction never varied, where dividing by the variance of 0 would give an infinity that reads as a number. Sums whose w is 0, from rows that all weigh 0, give null r2, ic, mse and rmse, since no row counts. There is no log_loss column under binary: SUM_FIELDS carries no sum for it. metrics() gives it, over a frame or a query.

Raises

TypeError for an s that is not a DataFrame; ValueError for one that lacks a column of SUM_FIELDS, such as the scores metrics() returns, and for a min_samples below 0; and WrongTypeError, both a TypeError and a ValueError, for a min_samples that is not a whole number.

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)
s = rp.eval.sums(out, spec, group="stock_id")          # ten numbers per stock
scores = rp.eval.from_sums(s)
for stock, n, r2 in scores.select("stock_id", "n", "r2").iter_rows():
    print(f"{stock}: {n} rows, r2 {r2:.3f}")
b0: 99 rows, r2 0.939
b1: 99 rows, r2 0.938
b2: 99 rows, r2 0.947
b3: 99 rows, r2 0.948

See also