rheon.polars.ModelBank.coef¶
- ModelBank.coef(spec: str | int | None = None, group: str | Iterable[str | None] | None = None) DataFrame[source]¶
Return the coefficients of every fit the bank holds, one row per coefficient.
Use it to read the coefficients from a bank, or from one loaded from a state file, without a row of data. For the coefficients on each row of a stream, read the
coeffield offit_predict()’s output, and for the sums behind a fit,gram().Parameters
specThe spec to read, by name or by position. The default,
None, reads every spec and skips each one that has no coefficients (see Raises). A bank whose specs have no coefficients at all gives an empty frame, with the same columns.groupThe group to read: one group key, as
groups()reports it, or a list of keys, withNonein the list for the null group. The default,None, reads every group. A key the bank has never seen gives no rows.
Returns
A
DataFramewith one row per (spec, group, instance, position), in spec order, then ingroups()’ order. It leads withspec, aslast_row(),summary()anddescribe()do. Every frame it returns has the same columns, so the frames of several banks stack with a plainpl.concat. The values are the fit after the last row each model learned, which the next row’spred_<t>is computed from. That is the fit the output’scoeffield shows on the rows where it is written. The columns:spec,group,instanceThe spec’s name, the group key as
groups()reports it, and the instance’s field suffix ("@h500", or""for a spec with one instance). An instance is the part of a model that fits one half-life of a grid.half_life,lamThe instance’s decay, as
rheon.spec.output_index()gives it: the half-life in clock units (inffor no decay), or thelam. The other one is null.weight_sumThe accumulated weight behind the fit, which the next row’s
weight_sumfield reports.position,target,ridge,feature_set,penaltyrheon.spec.coef_index()’s columns:positionindexes the flatcoeflist, and the others name the slot it belongs to.cluster,state,classcoef_index’s columns too: thekmeanscenter, thehmmstate or theew_classclass a position belongs to. Null for every other model.term"intercept", a feature’s name,"level"or"trend"forholt, or"rho"fordeco.coefThe coefficient, in the features’ original units. Null until the model’s first solve, and null wherever the model reports no number, as the
coeffield is on those rows: a fit withheld as too ill-conditioned to keep a digit, or anew_classclass no row has carried yet.
Raises
KeyErrorspecis a name the bank does not have.IndexErrorspecis a position the bank does not have.TypeErrorgroupis not a str, a list of them withNoneallowed, orNone.ValueErrorspecnames a spec whose model has no coefficients to index:ew_cov, which gives statistics;seqtest, which gives evidence;micro, whose number of clusters varies; andmarginal,rcov,corrchange,bocpdandaudit.
Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) bank = rp.ModelBank([spec]) bank.fit(df) coefs = bank.coef() # one row per coefficient for term, value in coefs.select("term", "coef").iter_rows(): print(f"{term}: {value:.2f}") # the truth: 1, 2 and -1
intercept: 0.99 x0: 1.97 x1: -1.00
To read one slot of a grid, filter on its columns, such as
coefs.filter(pl.col("ridge") == 0.1). To lay the terms out as columns, pivot the frame on every column that tells two fits apart:pivot("term", index=["spec", "group", "instance", "target", "ridge", "feature_set", "penalty", "cluster", "state", "class"], values="coef").See also
rheon.spec.coef_index(), the layout of a spec’scoeflist.last_row(), the whole output of each model’s last row.gram(), the Gram a fit is solved from.solve_failures(), the solves that needed help.
Notes
coefcan be filled beforepred_<t>is.pred_<t>waits formin_weight, whilecoefreads each solve the model has run, and a model’s solve schedule can run one sooner. For example, anewridgewith three coefficients,min_weight=10, a half-life of 100 and the default schedule carries coefficients on its third row, while itsweight_sumreads near 2.rheon.spec.ewridge()gives its schedule (solve_every). A coefficient read whileweight_sumis belowmin_weightcomes from fewer rows than the spec asks of a prediction.Sweeping every spec skips the ones with no coefficients, since the question was about the coefficients in the bank. Naming one of them still raises, since then the question was about that spec.