## Solve a multitude of linear least square system efficiently

Question

I have to find the best solution for >10^7 equation systems with 5 equations in 2 variables each (5 measurements to find 2 parameters with the least amount of error in a long series). The following code (normally used to do curve fitting) does what I want:

```
#Create_example_Data
n = 100
T_Arm = np.arange(10*n).reshape(-1, 5, 2)
Erg = np.arange(5*n).reshape(-1, 5)
m = np.zeros(n)
c = np.zeros(n)
#Run
for counter in xrange(n):
m[counter], c[counter] = np.linalg.lstsq(T_Arm[counter, :, :],
Erg[counter, :])[0]
```

Unfortunately it is too slow. Is there any way how to speed this code up significantly? I tried to vectorise it, but I did not succeed. Using the last solution as a initial guess might be a good idea as well. Using `scipy.optimize.leastsq`

did not speed it up as well.

Show source

## Answers to Solve a multitude of linear least square system efficiently ( 1 )

You could use a sparse block matrix A which stores the (5, 2) entries of T_Arm on its diagonal, and solve AX = b where b is the vector composed of stacked entries of

`Erg`

. Then solve the system with scipy.sparse.linalg.lsqr(A, b).To construct A and b I use n=3 for visualisation purposes:

And then

EDIT: A is not as huge in memory as it seems: more on block sparse matrices here.