[SciPy-User] fitting multiple coefficients

Keith Goodman kwgoodman@gmail....
Sun Jul 25 11:59:12 CDT 2010


On Sun, Jul 25, 2010 at 9:48 AM, eneide.odissea
<eneide.odissea@gmail.com> wrote:
> Hi all
> I have the following function
> y = m[0] A + m[1] B + m[2] C
> and I have the following data
> A   ;   B  ;     C   ;    y
> 10    ;  122 ;  7   ;    4
> 12    ;  134 ;  9   ;    2
> 11    ;  131 ;  6   ;    4
> 7      ;  180 ;  4   ;    2
> Do you know what function of python I could use to find
> the weights m[i] that  best fit for these data?

>> import numpy as np
>> x = np.array([[10, 122, 7], [12, 134, 9], [11, 131, 6], [7, 180, 4]])
>> y = np.array([4, 2, 4, 2])
>> m, ig1, ig2, ig3 = np.linalg.lstsq(x, y)
>> m
   array([ 0.84562416, -0.00455408, -0.74598866])
>> np.dot(x, m) # <-- fitted y
   array([ 2.67872369,  2.82334576,  4.22934983,  2.11568083])


More information about the SciPy-User mailing list