Linear regression in 3 dimensions

David J. Braden dbraden at invalid.add
Wed Sep 13 22:44:12 EDT 2006


Andrew McLean wrote:
> Bernhard,
> 
> Levenberg-Marquardt is a good solution when you want to solve a general 
> non-linear least-squares problem. As Robert said, the OPs problem is 
> linear and Robert's solution exploits that. Using LM here is unnecessary 
> and I suspect a fair bit less efficient (i.e. slower).
> 
> - Andrew
> 
> 
> bernhard.voigt at gmail.com wrote:
>> Hi Robert,
>>
>> I'm using the scipy package for such problems. In the submodule
>> scipy.optimize there is an implmentation of a least-square fitting
>> algorithm (Levenberg-Marquardt) called leastsq.
>>
>> You have to define a function that computes the residuals between your
>> model and the data points:
>>
>> import scipy.optimize
>>
>> def model(parameter, x, y):
>>     a, b, c = parameter
>>     return a*x + b*y + c
>>
>> def residual(parameter, data, x, y):
>>     res = []
>>     for _x in x:
>>         for _y in y:
>>             res.append(data-model(parameter,x,y)
>>     return res
>>
>> params0 = [1., 1.,1.]
>> result = scipy.optimize.leastsq(resdiual, params0, (data,x,y))
>> fittedParams = result[0]
>>
>> If you haven't used numeric, numpy or scipy before, you should take a
>> look at an introduction. It uses some nice extended array objects,
>> where you can use some neat index tricks and compute values of array
>> items without looping through it.
>>
>> Cheers! Bernhard
>>

<<snip>>

Hi Bernhard,
I am just starting to learn Python; could you plz tell me specifically 
the introduction(s) you have in mind?

TIA,
DaveB



More information about the Python-list mailing list