cupy.linalg.lstsq(a, b, rcond=1e-15)[source]

Return the least-squares solution to a linear matrix equation.

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 linearly independent rows of a can be less than, equal to, or greater than its number of linearly independent columns). If a is square and of full rank, then x (but for round-off error) is the “exact” solution of the equation.

  • a (cupy.ndarray) – “Coefficient” matrix with dimension (M, N)

  • b (cupy.ndarray) – “Dependent variable” values with dimension (M,) or (M, K)

  • rcond (float) – Cutoff parameter for small singular values. For stability it computes the largest singular value denoted by s, and sets all singular values smaller than s to zero.


A tuple of (x, residuals, rank, s). Note x is the least-squares solution with shape (N,) or (N, K) depending if b was two-dimensional. The sums of residuals is the squared Euclidean 2-norm for each column in b - a*x. The residuals is an empty array if the rank of a is < N or M <= N, but iff b is 1-dimensional, this is a (1,) shape array, Otherwise the shape is (K,). The rank of matrix a is an integer. The singular values of a are s.

Return type



This function calls one or more cuSOLVER routine(s) which may yield invalid results if input conditions are not met. To detect these invalid results, you can set the linalg configuration to a value that is not ignore in cupyx.errstate() or cupyx.seterr().