title: Least square optimization with bounds using scipy.optimize
body: <p>I have a least square optimization problem that I need help solving. So far, I have some code that does the following function: </p>
  
  <pre><code>shankarFunc = lambda p, x: p[0] * (1 - np.exp(-1 * ((x / p[1]) ** p[2])))
  errFunc = lambda p, x, y: shankarFunc(p, x) - y
  
  x0 = [max(y), 100 / max(y), 1]
  
  p1, success = optimize.leastsq(errFunc, x0, args=(x, y))
  </code></pre>
  
  <p>and this <em>almost</em> works for all cases. It doesn't converge for some data, so I need to do a least square fitting <strong>with</strong> bounds for the fitted parameters. I then noticed that optimize.minimize has bounds, and tried to use that instead (minimizing the error function). </p>
  
  <pre><code>shankarFunc = lambda p, x: p[0] * (1 - np.exp(-1 * ((x / p[1]) ** p[2])))
  errFunc = lambda p, x, y: shankarFunc(p, x) - y
  
  x0 = [max(y), 100 / max(y), 1]
  b0 = (0, max(y) + 2)
  b1 = (1e-4, 5)
  b2 = (0, None)
  p1 = optimize.minimize(errFunc, x0, args=(x, y), bounds=(b0, b1, b2))
  </code></pre>
  
  <p>This gives me problems, as it returns the following error: </p>
  
  <pre><code>  File "C:\Anaconda\lib\site-packages\scipy\optimize\optimize.py", line 610, in
  approx_fprime
      grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]
  ValueError: setting an array element with a sequence.
  </code></pre>
  
  <p>I've tried any number of variations on the input, but can't get rid of that.</p>
  
  <p>How can I get optimize.minimize to play along? Is there a better solution to add bounds to the solution?</p>