rheon.gram.subset¶
- rheon.gram.subset(g: Gram, columns: Sequence[str | int]) Gram[source]¶
Keep some of a Gram’s columns, in the order given.
Use it to fit or diagnose a narrower design from a Gram that holds more columns: forward stepwise selection, an information criterion over feature sets, or an
r-column fit read off ak-column stream, all from one pass. To narrow one fit,solve(),lasso_path(),lars_path()andcoef_stats()takefeaturesinstead.Parameters
gThe Gram, as
gram()returns it.columnsThe columns to keep, by name or by position, as the Gram’s own
columnskey names them. A negative position counts from the end. The intercept may be kept like any other column. Leave the intercept out, and every fit on the subset goes through the origin, as a spec withfit_intercept=Falsedoes:solve()and the paths then read the raw moments,E[z z']andE[z y], and center nothing. Keep it for a fit with an intercept. The keyword’s older name,cols, is refused, namingcolumns.
Returns
A
Gramof the columns kept: itscolumns,means,comoments,cross_moments,means_by_target,cross_centeredandlag_comomentsnarrowed to them, itscomomentsin float64, packed ifg’s is. The targets are untouched, since they index a different axis.Raises
KeyErrorA name the Gram does not have.
IndexErrorA position outside the Gram’s columns.
TypeErrorcolumnsand its older namecolsare given together.ValueErrorA column named twice, by name or by position, since two copies of one column make the subset’s matrix singular; an array not sized to the Gram’s columns and targets, such as a
meansof one entry.
Examples
This code reads
dffrom Example data:spec = rp.spec.ewridge( "ridge", targets=["y"], features=["x0", "x1", "x2"], half_life=100.0 ) bank = rp.ModelBank([spec]) bank.fit_predict(df) g = bank.gram("ridge")[0] narrow = rp.gram.subset(g, ["intercept", "x0"]) # the fit on x0 alone print(narrow["columns"], rp.gram.solve(narrow).round(2))
['intercept', 'x0'] [1.03 1.81]
See also
solve(), whosefeaturesnarrows one fit without a new Gram.Every least-squares fit is a function of the Gram in docs/GRAM.md, the theory and a recipe that runs.
Notes
The subset is exact. The moments of some of the columns are a sub-block of the moments of all of them, so this selects that block, and a regression on the subset is the regression the whole Gram implies for those columns.