[SciPy-user] optimize.leastsq()

Francesc Alted falted at pytables.org
Fri Oct 22 08:03:44 EDT 2004


Hi James,

A Dijous 21 Octubre 2004 22:12, James R. Phillips va escriure:
> Here is a different simple scipy least squares fit:
> 
> http://www.scipy.org/mailinglists/mailman?fn=scipy-user/2004-January/002452.html

Thanks for your suggestion. Here is another version of your code, just a bit
more 'vectorized':

-----------------------------------------------------------------------
from scipy import *

xdata = array([5.357, 5.457, 5.797, 5.936, 6.161, 6.697, 6.731, 6.775, 8.442, 9.769, 9.861])
ydata = array([0.376, 0.489, 0.874, 1.049, 1.327, 2.054, 2.077, 2.138, 4.744, 7.068, 7.104])
transpose(array([[1]*11, xdata, xdata*xdata]))  # y = 1 + x + x*x
coeffs = linalg.basic.lstsq(matrix, ydata)[0]

print "scipy.linalg.basic.lstsq curve fitting example"
print "fitting data to quadratic equation y = a + bx + cx^2"
ycalc = coeffs[0] + coeffs[1] * xdata + coeffs[2] * xdata * xdata
error = ycalc - ydata
print "yields:  x data     y data    calc value   error"
for i in range(len(xdata)):
    print "         % .3f    % .3f      % .3f    % .3f" % (xdata[i], ydata[i], ycalc[i], error[i])
print
-------------------------------------------------------------------------

Regards,

-- 
Francesc Alted




More information about the SciPy-User mailing list