rheon.gram.from_row¶
- rheon.gram.from_row(row: Any) Gram[source]¶
Read a closed group’s row as the Gram
gram()would return.Use it on the rows
closed_groups()writes, or on those rows read back from a file, so that every function in this module works on them. For a group still open in the bank, usegram()itself.Parameters
rowOne closed group’s row: a one-row frame of
closed_groups(), a row of itsiter_rows(named=True), or a mapping. A closed group writes one row per Gram, so a row’stargetsare the targets of that one Gram. A row that namescross_centeredby its old British spelling,cross_centred, is refused (see Raises).
Returns
A
Gram. The row’scomomentsis the upper triangle with the diagonal, row by row, and itscross_moments,means_by_targetandcross_centeredare row-major(n_targets, k), withkthe number of columns. This function expands all four.Raises
ValueErrorWhen the row is of a model that keeps no Gram (its
columnsis null), when a frame holds more or fewer than one row, or whencomomentsdoes not holdk (k + 1) / 2entries.TypeErrorWhen
rowis none of the three forms, or namescross_centred, the old spelling ofcross_centered.
Examples
This code reads
dffrom Example data:blocks = rp.spec.ew_cov( "cov", features=["x0", "x1"], lam=1.0, group="block", group_close="monotone" ) bank = rp.ModelBank([blocks]) bank.fit_predict(df.with_columns(block=pl.int_range(pl.len()) // 100)) closed = bank.closed_groups() # one row per closed block for row in closed.iter_rows(named=True): g = rp.gram.from_row(row) r = rp.gram.correlation(g) print(row["group"], round(float(r[0, 1]), 3))
0 0.021 1 0.108 2 -0.053
See also
closed_groups(), which writes the rows.Read Grams from saved files, a recipe that runs, with numpy alone too.
Notes
The result is what
gram()would have returned for that group, bit for bit. The model keeps one triangle of its co-moment matrix and reads the other through its mirror, so the upper triangle the row holds loses nothing, and keeping only that triangle makes the row about half the size of the whole matrix.