First, lets write the equations as matrices on the format
, where![](/wiki/plugins/servlet/confluence/placeholder/unknown-macro?name=mathinline&locale=en_US&version=2)
To solve the problem in NumPy, we will use the function numpy.linalg.lstsq (or with SciPys scipy.linalg.lstsq) which is currently not described in Matrices and linear algebra. We recommend you to read the function documentation before proceeding.
import numpy as np
# Defining the matrices
A = np.array([[1, 2],
[3, 4],
[5, 6]])
b = np.array([[1],[2],[3]])
x, residuals, rank, s = np.linalg.lstsq(A, b, rcond=None)
# "rcond=None" sets new default for rcond, see function documentation
print(x) # "x" is the solution
# Output:
[[-5.97106181e-17]
[ 5.00000000e-01]]
We can interpret this such as the least squares solution is![](/wiki/plugins/servlet/confluence/placeholder/unknown-macro?name=mathinline&locale=en_US&version=2)