title: How to find parameters of an optimization function by using scipy?
body: <p>I'm trying to replicate this paper: <a href="http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1390998" rel="nofollow noreferrer">Global motion estimation from coarsely sampled motion vector field and the applications</a></p>
  <p>I need to find the parameters <code>m0, m1, m2....., m7</code>
  Given a images x1_1 and x1_2 and where the equations are</p>
  <p><a href="http://pastebin.com/VutTTTSM" rel="nofollow noreferrer">Example data.</a></p>
  <p><a href="https://i.stack.imgur.com/7tmgq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7tmgq.png" alt="enter image description here" /></a></p>
  <p>Where x'{x1_2[1, :, :]} and y'{x1_2[0, :, :]} are values of x1_2 and
  x, y the x1_1 is x1_1 in the same style.</p>
  <p>I have refereed to the example in <a href="https://stackoverflow.com/questions/26242979/fit-multiple-parametric-curves-with-scipy">this post</a> to make this implementation.</p>
  <p>Could anyone help me how to find these parameters?</p>
  <h1>Made edits as per comments and the <a href="http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html#least-square-fitting-leastsq" rel="nofollow noreferrer">example of <code>leastsq</code> function</a>.</h1>
  <h1>Changed program is given below and output is given below</h1>
  <pre><code>import os
  import sys
  import numpy as np
  import scipy
  from scipy.optimize import leastsq
  
  
  def peval (inp_mat,p):
      m0,m1,m2,m3,m4,m5,m6,m7 = p
      out_mat = np.zeros(inp_mat.shape,dtype=np.float32)
      mid = inp_mat.shape[0]/2
      for xy in range(0,inp_mat.shape[0]):
          if (xy&lt;(inp_mat.shape[0]/2)):
              out_mat[xy] = ( (  (inp_mat[xy+mid]*m0)+(inp_mat[xy]*m1)+ m2 ) /( (inp_mat[xy+mid]*m6)+(inp_mat[xy]*m7)+1 ) )
          else:
              out_mat[xy] = ( (  (inp_mat[xy]*m3)+(inp_mat[xy-mid]*m4)+ m5 ) /( (inp_mat[xy]*m6)+(inp_mat[xy-mid]*m7)+1 ) )
      return out_mat
  
  def residuals(p, out_mat, inp_mat):
      m0,m1,m2,m3,m4,m5,m6,m7 = p
      err=np.zeros(inp_mat.shape,dtype=np.float32)
      if (out_mat.shape == inp_mat.shape):
          for xy in range(0,inp_mat.shape[0]):
              err[xy] =  err[xy]+ (out_mat[xy] -inp_mat[xy])
          return err
  
  
  
  
  f = open('/media/anilil/Data/Datasets/repo/txt_op/vid.txt','r')
  x = np.loadtxt(f,dtype=np.int16,comments='#',delimiter='\t')
  nof = x.shape[0]/72 # Find the number of frames
  x1 = x.reshape(-1,60,40)
  x1_1= x1[0,:,:].flatten()
  x1_2= x1[1,:,:].flatten()
  
  x= []
  y= []
  
  for xy in range(1,50,1):
      y.append(x1[xy,:,:].flatten())
      x.append(x1[xy-1,:,:].flatten())
  
  x=np.array(x,dtype=np.float32)
  y=np.array(y,dtype=np.float32)
  length =  x1_1.shape#initail guess
  p0 = np.array([1,1,1,1,1,1,1,1],dtype=np.float32)
  
  abc=leastsq(residuals, p0,args=(y,x))
  print ('Size of first matrix is '+str(x1_1.shape))
  print ('Size of first matrix is '+str(x1_2.shape))
  
  print (&quot;Done with program&quot;)
  </code></pre>
  <h1>Output</h1>
  <pre><code>ValueError: object too deep for desired array
  Traceback (most recent call last):
    File &quot;/media/anilil/Data/charm/mv_clean/.idea/nose_reduction_mpeg.py&quot;, line 49, in &lt;module&gt;
      abc=leastsq(residuals, p0,args=(y,x))
    File &quot;/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py&quot;, line 378, in leastsq
      gtol, maxfev, epsfcn, factor, diag)
  minpack.error: Result from function call is not a proper array of floats.
  </code></pre>