polars_online.gram.from_row

polars_online.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, use gram() itself.

Parameters

row

One closed group’s row: a one-row frame of closed_groups(), a row of its iter_rows(named=True), or a mapping. A closed group writes one row per Gram, so a row’s targets are the targets of that one Gram. A row that names cross_centered by its old British spelling, cross_centred, is read with a warning (see Raises).

Returns

A Gram. The row’s comoments is the upper triangle with the diagonal, row by row, and its cross_moments, means_by_target and cross_centered are row-major (n_targets, k), with k the number of columns. This function expands all four.

Raises

ValueError

When the row is of a model that keeps no Gram (its columns is null), when a frame holds more or fewer than one row, or when comoments does not hold k (k + 1) / 2 entries.

TypeError

When row is none of the three forms, or names both cross_centered and cross_centred.

PolarsOnlineDeprecationWarning

A warning, not an error: issued when the row names cross_centred. That spelling is read until the next major version.

Examples

This code reads df from Example data:

blocks = po.spec.ew_cov(
    "cov", features=["x0", "x1"], lam=1.0, group="block", group_close="monotone"
)
bank = po.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 = po.gram.from_row(row)
    r = po.gram.correlation(g)
    print(row["group"], round(float(r[0, 1]), 3))
0 0.021
1 0.108
2 -0.053

See also

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.