title: Simultaneously fit linearly every line of a 2d numpy array
body: <p>I am working in Python on image analysis. I have an image (2d numpy array) with some intensity drift in it. I want to level it. </p>
  
  <p>To remove the increasing/decreasing intensity over the width of the image, I want to fit every row of the 2d numpy array with a line. I however do not want to loop through every row index. </p>
  
  <p>MWE:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  
  width=1500
  height=2500
  np.random.random((width,height))
  fill_fun = lambda x,a,b : a*x+b
  play_image = fill_fun(np.tile(np.arange(width),(height,1)),0.15,2)+np.random.random( (height,width) )
  
  #For representation purposes:
  
  #plt.imshow(play_image,cmap='Greys_r')
  #plt.show()
  
  
  #1) Fit every row and kill the intensity decrease/increase tendency
  fit_func = lambda p,x: p[0]*x+b  
  errfunc = lambda p, x, y: abs(fitfunc(p, x) - y) # Distance to the target function
  x_axis=np.linspace(0,width,width)
  
  for i in range(height):
      row_val=play_image[i,:]
      p0=[(row_val[-1]-row_val[0])/float(width),row_val[0]] #guess
      p1, success = optimize.leastsq(errfunc, p0[:], args=(x_axis,row_val))
      play_image[i,:]-= fit_func(p1,x_axis)-p1[1]
  </code></pre>
  
  <p>By doing this I effectively level my image intensity horizontally. Is there anyway I can replace the loop by a matrix operation ? To somehow fit all the lines at the same time with a (height,2) parameter vector ? </p>
  
  <p>Thanks for the help</p>