mojolearn.lstsq#

mojolearn.lstsq(a, b, rcond=None, *, numeric_mode=None)#

Least-squares solution of a @ x = b.

Follows numpy.linalg.lstsq, solved through the SVD of a. Singular values at or below rcond * s.max() are treated as zero.

Parameters:
  • a (array-like of shape (M, N))

  • b (array-like of shape (M,) or (M, K))

  • rcond (float or None, default None) – Relative cutoff for small singular values. None uses float32 machine epsilon times max(M, N).

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this call. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

Returns:

  • x (ndarray of shape (N,) or (N, K), float32)

  • residuals (ndarray of shape (K,) or (0,), float32) – Squared column norms of b - a @ x when rank == N < M; otherwise empty.

  • rank (int) – Effective rank of a.

  • s (ndarray of shape (min(M, N),), float32) – Singular values of a, descending.

Raises:

ValueError – If a and b have different numbers of rows.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> A = rng.random((100, 4), dtype=np.float32)
>>> x, res, rank, s = mojolearn.lstsq(A, rng.random(100, dtype=np.float32))