MCPcopy Create free account
hub / github.com/dask/dask / lstsq

Function lstsq

dask/array/linalg.py:1396–1462  ·  view source on GitHub ↗

Return the least-squares solution to a linear matrix equation using QR decomposition. Solves the equation `a x = b` by computing a vector `x` that minimizes the Euclidean 2-norm `|| b - a x ||^2`. The equation may be under-, well-, or over- determined (i.e., the number of

(a, b)

Source from the content-addressed store, hash-verified

1394
1395
1396def lstsq(a, b):
1397 """
1398 Return the least-squares solution to a linear matrix equation using
1399 QR decomposition.
1400
1401 Solves the equation `a x = b` by computing a vector `x` that
1402 minimizes the Euclidean 2-norm `|| b - a x ||^2`. The equation may
1403 be under-, well-, or over- determined (i.e., the number of
1404 linearly independent rows of `a` can be less than, equal to, or
1405 greater than its number of linearly independent columns). If `a`
1406 is square and of full rank, then `x` (but for round-off error) is
1407 the "exact" solution of the equation.
1408
1409 Parameters
1410 ----------
1411 a : (M, N) array_like
1412 "Coefficient" matrix.
1413 b : {(M,), (M, K)} array_like
1414 Ordinate or "dependent variable" values. If `b` is two-dimensional,
1415 the least-squares solution is calculated for each of the `K` columns
1416 of `b`.
1417
1418 Returns
1419 -------
1420 x : {(N,), (N, K)} Array
1421 Least-squares solution. If `b` is two-dimensional,
1422 the solutions are in the `K` columns of `x`.
1423 residuals : {(1,), (K,)} Array
1424 Sums of residuals; squared Euclidean 2-norm for each column in
1425 ``b - a*x``.
1426 If `b` is 1-dimensional, this is a (1,) shape array.
1427 Otherwise the shape is (K,).
1428 rank : Array
1429 Rank of matrix `a`.
1430 s : (min(M, N),) Array
1431 Singular values of `a`.
1432 """
1433 q, r = qr(a)
1434 x = solve_triangular(r, q.T.conj().dot(b))
1435 residuals = b - a.dot(x)
1436 residuals = abs(residuals**2).sum(axis=0, keepdims=b.ndim == 1)
1437
1438 token = tokenize(a, b)
1439
1440 # r must be a triangular with single block
1441
1442 # rank
1443 rname = "lstsq-rank-" + token
1444 rdsk = {(rname,): (np.linalg.matrix_rank, (r.name, 0, 0))}
1445 graph = HighLevelGraph.from_collections(rname, rdsk, dependencies=[r])
1446 # rank must be an integer
1447 rank = Array(graph, rname, shape=(), chunks=(), dtype=int)
1448
1449 # singular
1450 sname = "lstsq-singular-" + token
1451 rt = r.T.conj()
1452 sdsk = {
1453 (sname, 0): (

Callers

nothing calls this directly

Calls 9

ArrayClass · 0.90
meta_from_arrayFunction · 0.90
qrFunction · 0.85
solve_triangularFunction · 0.85
from_collectionsMethod · 0.80
tokenizeFunction · 0.50
dotMethod · 0.45
conjMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected