polars_online.eval.from_sums¶
- polars_online.eval.from_sums(s: DataFrame, *, min_samples: int = 30, binary: bool = False) DataFrame[source]¶
Read
metrics()’ scores from the sumssums()made.Use it last, after
sums()has reduced each chunk of a stream andmerge_sums()has added them, to get the scoresmetrics()would give over every row at once.Parameters
sA frame
sums()ormerge_sums()returned.min_samplesThe fewest rows a key is scored on, 30 by default. A key with fewer rows keeps its row, with
nfilled and the scores null, asmetrics()keeps it. It counts rows,n, whatever they weigh: a row of weight 0 counts (seesums()’weight).binarySay that the sums were made under
sums()’binary, and name the columns asmetrics()names them under it:brier_skillforr2andaccuracy(at 0.5) forhit_rate.Falseby default. The sums do not record their reading, so this cannot read it from them: which readinghit_rateis was fixed when the sums were made, since this divideshitsbysigned.
Returns
A
DataFrameof the keys andmetrics()’ 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,mseandrmse. Sums made withsums()’weightgive each score weighted,mseassse / w.r2is null where the target never varied, andicwhere 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 whosewis 0, from rows that all weigh 0, give nullr2,ic,mseandrmse, since no row counts. There is nolog_losscolumn underbinary:SUM_FIELDScarries no sum for it.metrics()gives it, over a frame or a query.Raises
TypeErrorfor ansthat is not aDataFrame;ValueErrorfor one that lacks a column ofSUM_FIELDS, such as the scoresmetrics()returns, and for amin_samplesbelow 0; andWrongTypeError, both aTypeErrorand aValueError, for amin_samplesthat is not a whole number.Examples
This code reads
dffrom Example data:spec = po.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) out = po.ModelBank([spec]).fit_predict(df) s = po.eval.sums(out, spec, group="stock_id") # ten numbers per stock scores = po.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
sums()andmerge_sums(), which make the sums.metrics(), the same scores in one call, over a frame or a query.