# Constrained least-squares fitting with Python      Scipy contains a good least-squares fitting routine, leastsq(), which implements a modified Levenberg-Marquardt algorithm.  I just learned that it also has a constrained least-squared routine called fmin_slsqp().   I am using simple upper and lower bound constraints, but it’s also possible to specify more complex functional constraints.
What I did not realize, at first, is that fmin_slsqp requires a different type of objective function than leastsq.   leastsq requires you to write a function that returns a vector of residuals, and leastsq automatically squares and sums the residuals.  fmin_slsqp is actually more flexible, in that it can use any objective function that returns a single scalar value.  To implement least-squares curve fitting, your objective function will need to find the residual at each data point, square the values, and sum them up.  Hopefully this tip will save you some time.
Check out the scipy optimization tutorial for more examples.  Here is the original paper by Dieter Kraft which introduces the algorithm used by fmin_slsqp.

### 7 thoughts on “Constrained least-squares fitting with Python”

1. Thank you! this little post of you saved me a long day of fiddling.
cheers
H.

2. Thanks for your post! You saved my time, too.

3. 4. I am also facing a similar problem. In my case the matrix equation looks like [Y]nX1=[X]nXm[P]mX1, where Y and P are vectors and X is a matrix. Further, there is a equality constraint on P which is Sum(P(i))=0.0. How do I proceed to solve that? Which function of python is suitable for this? I saw few of discussion on scipy.optimize.fmin_slsqp() function. However, I need your help. I am new in SCIPY. Please help me out in this regard.

5. You are on the right path. You can either use scipy.optimize.fmin_slsqp directly, or use scipy.optimize.minimize with method=’SLSQP’. See the docs at:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
You can set equality constraints using the constraints keyword argument, which also allows you to provide a Python function that defines the constraint. In your case, this function would just sum the elements of P.

This site uses Akismet to reduce spam. Learn how your comment data is processed.