Skip to content

lazymatrix.LazyMatrix ​

LazyMatrix for Python · 0.1.1

Source

python
LazyMatrix(X: typing.Any, *, center: bool = True, scale: bool = True)

An owned matrix with lazy column centering and scaling.

Parameters ​

X : numpy.ndarray or scipy.sparse matrix or array

Two-dimensional real numeric input. Sparse input must use CSR or CSC storage. Values are copied to float64 storage, and sparse copies are converted to canonical CSC. Later changes to X do not affect this object.

center : bool, default=True

Subtract each column's mean during matrix operations.

scale : bool, default=True

Divide each column by its population standard deviation. Exact zero scales are replaced with one.

Notes ​

Products and column access do not allocate a full normalized matrix. Dense input still requires a full raw-data copy. Multiple right-hand sides currently use repeated vector products. Nonfinite values follow the Rust core's IEEE arithmetic; missing values are not ignored.

Use toarray for explicit dense conversion and aslinearoperator for SciPy solvers. General slicing, mutation, elementwise arithmetic, and implicit NumPy conversion are unsupported.

Examples ​

python
>>> import numpy as np
>>> from lazymatrix import standardize
>>> Z = standardize(np.array([[1.0, 0.0], [3.0, 4.0]]))
>>> Z @ np.array([1.0, 2.0])
array([-3.,  3.])
>>> Z[:, 0]
array([-1.,  1.])

__init__ {#init} ​

python
__init__(X: typing.Any, *, center: bool = True, scale: bool = True) -> None

Source

shape ​

python
shape(self) -> tuple[int, int]

Source

The logical number of rows and columns.

ndim ​

python
ndim(self) -> int

Source

The number of dimensions, always two.

dtype ​

python
dtype(self) -> numpy.dtype[numpy.float64]

Source

The stored and output data type, always float64.

centers ​

python
centers(self) -> NDArray[np.float64] | None

Source

A copy of the original columns' fitted means, or None.

Transposing the matrix retains these original column parameters.

scales ​

python
scales(self) -> NDArray[np.float64] | None

Source

A copy of the original columns' fitted scales, or None.

Transposing the matrix retains these original column parameters.

T ​

python
T(self) -> "LazyMatrix"

Source

A transpose sharing the same raw storage and fitted parameters.

aslinearoperator ​

python
aslinearoperator() -> scipy.sparse.linalg.LinearOperator

Source

Expose this matrix to SciPy's iterative linear algebra routines.

Returns ​

scipy.sparse.linalg.LinearOperator

An operator sharing this matrix's storage and supporting forward, transpose, and multiple-right-hand-side products.

Examples ​

python
>>> import numpy as np
>>> from scipy.sparse.linalg import lsqr
>>> from lazymatrix import standardize
>>> Z = standardize(np.array([[1.0], [3.0]]))
>>> lsqr(Z.aslinearoperator(), np.array([-2.0, 2.0]))[0]
array([2.])

toarray ​

python
toarray() -> numpy.typing.NDArray[numpy.float64]

Source

Materialize the logical matrix as an independent dense array.

Returns ​

numpy.ndarray

A float64 array with this matrix's shape. This operation allocates storage for every logical entry, including normalized sparse zeros.

Released under the MIT and Apache 2.0 licenses.