# [SciPy-User] leastsq returns bizarre, not fitted, output for float values

Matthieu Rigal rigal@rapideye...
Thu Jun 10 09:02:23 CDT 2010

```OK, I've found the bug...

Somehow the leastsq function is not working if both data sets are float 32
type.
By just adding following line the problem is solved :
aX = numpy.asarray(aX, dtype=numpy.float64)

Is it a known bug ? Should I add it to the bug tracker ?

Best regards,
Matthieu

On Thursday 10 June 2010 14:57:10 Matthieu Rigal wrote:
> Hi folks,
>
> message...
>
> I am using leastsq for several things, but it is returning strange
> values for one of the case I'm using it for. I simplified it to the
> following code I'll paste below.
> The effect is that it is fitting nothing, just giving back the
> parameters given for initialization. Thus some fitting is possible as
> you will see in the plotted graph.
> I'm using SciPy 0.7.
>
> It might be a bug, a misusage from my side... or some data type
> incompatibility I was not able to find on the net or in the source...
>
> As you will see, if you transform the x-data to a numpy.int array (by
> uncommenting a line below), the fitting is working... is it to be
> expected ? It should then be somewhere in the doc, isn't it ?
>
>
> import numpy
> from scipy.optimize import leastsq
> import matplotlib.pyplot as plt
>
> def LinearFit(p, y, x):
>     a, b = p
>     return y - (a*x +b)
>
> aX = numpy.asarray([ 22.08742332,  23.43987274,  21.59165192,
> 24.80192566, 26.11182976,  29.18944931,  27.89473152,  30.00043106,
>         36.24227142,  30.45967293,  30.04778099,  28.11702538,
>         29.31716728,  27.89473152,  20.59804916,  34.19070053,
>         48.33156204,  50.82163239,  45.22343063,  42.80136108,
>         30.71160889,  29.31716728,  25.14836884,  23.50605965,
>         26.89011765,  40.35306168,  55.074543  ,  58.57307816,
>         60.77198792,  56.14603043,  39.29994583,  38.14756012,
>         35.76476288,  27.31066895,  23.45325851,  30.46047974,
>         37.53346634,  41.04254532,  54.47524643,  61.14104462,
>         61.03421402,  56.14603043,  44.67305756,  35.13313675],
> dtype=numpy.float32)
>
> aY = numpy.asarray([ 25.45091248,  25.50468063,  27.15722656,
> 25.10549927, 28.44662094,  30.3882637 ,  31.90523148,  34.12581253,
>         36.62049484,  33.90032196,  34.04083252,  29.66094398,
>         30.68564224,  29.31051826,  25.17509079,  37.28609848,
>         42.86494827,  48.25041199,  46.88908005,  34.44023132,
>         31.26217461,  31.8005867 ,  28.34657669,  26.77126312,
>         31.06710815,  41.03251266,  49.48557281,  52.79579926,
>         50.865448  ,  48.03937531,  39.30026245,  38.50889969,
>         37.07154083,  31.61130905,  27.42698288,  30.84166718,
>         30.84166718,  40.47367096,  50.37258148,  53.13900757,
>         53.75816727,  52.74428177,  43.87319183,  33.70808029],
> dtype=numpy.float32)
>
> #aX = numpy.asarray(numpy.rint(aX), dtype=numpy.int)
>
> p0 = [1.0] + [aY.min()-aX.min()]
> aParams, err, i, j, k = leastsq(LinearFit, p0,  args=(aY, aX),
> maxfev=10000,full_output=True)
> aY0 = aParams[0] * aX + aParams[1]
> print err, i, j, k
> print aParams
>
> plt.plot(aX, aY, '+', aX, aY0, '+')
> plt.legend(['input','model'])
> plt.show()
>
>
> Thanks in advance for the help,
> Best Regards,
> M

--
Matthieu Rigal
Product Development

RapidEye AG                           Tel: +49-(0)3381-89 04 331
Molkenmarkt 30                       Fax: +49-(0)3381-89 04 101
14776 Brandenburg/Havel
Germany                                  http://www.rapideye.de

RapidEye AG
Molkenmarkt 30
14776 Brandenburg an der Havel
Germany

Head Office/Sitz der Gesellschaft: Brandenburg an der Havel
Management Board/Vorstand: Wolfgang G. Biedermann
Chairman of Supervisory Board/Vorsitzender des Aufsichtsrates:
Juergen Breitkopf
Commercial Register/Handelsregister Potsdam HRB 17 796
Tax Number/Steuernummer: 048/100/00053
VAT-Ident-Number/Ust.-ID: DE 199331235
DIN EN ISO 9001 certified

*************************************************************************
Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese
E-Mail irrtuemlich erhalten haben, informieren Sie bitte sofort den
Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie
die unbefugte Weitergabe dieser E-Mail ist nicht gestattet.

The information in this e-mail is intended for the named recipients
only. It may contain privileged and confidential information. If you
have received this communication in error, any use, copying or
dissemination of its contents is strictly prohibited. Please erase all
copies of the message along with any included attachments and notify
RapidEye AG or the sender immediately by telephone at the number