title: Python lambda function with arrays as parameters
body: <p>I am trying to define a function of n variables to fit to a data set. The function looks like this.</p>
  
  <p><a href="https://i.stack.imgur.com/r9QPv.png" rel="nofollow noreferrer">Kelly Function</a></p>
  
  <p>I then want to find the optimal ai's and bj's to fit my data set using scipy.optimize.leastsq</p>
  
  <p>Here's my code so far.</p>
  
  <pre><code>from scipy.optimize import leastsq 
  import numpy as np
  
  def kellyFunc(a, b, x): #Function to fit.
    top = 0
    bot = 0
    a = [a]
    b = [b]
    for i in range(len(a)):
      top = top + a[i]*x**(2*i)
      bot = bot + b[i]*x**(2*i)
    return(top/bot)
  
  
  def fitKelly(x, y, n):
    line = lambda params, x : kellyFunc(params[0,:], params[1,:], x) #Lambda Function to minimize
    error = lambda params, x, y : line(params, x) - y #Kelly - dataset
  
    paramsInit = [[1 for x in range(n)] for y in range(2)] #define all ai and bi = 1 for initial guess
  
    paramsFin, success = leastsq(error, paramsInit, args = (x,y)) #run leastsq optimization
  
    #line of best fit
    xx = np.linspace(x.min(), x.max(), 100)
    yy = line(paramsFin, xx)
  
    return(paramsFin, xx, yy)
  </code></pre>
  
  <p>At the moment it's giving me the error:<br>
  "IndexError: too many indices" because of the way I've defined my initial lambda function with params[0,:] and params[1,:]. </p>