Aleksander_B
Aleksander_B

Reputation: 163

scipy.optimize curve_fit returns wrong value (depends on the machine)

I wrote some code with scipy.optimize curve_fit. It works perfectly on my computer:

Windows 7 Home Premium with Service Pack 1, 64bit Dell Studio 1558 Intel Core i3 cpu [email protected] 2.13GHz, 3.86 GB of RAM Python 2.7.3 (default, Apr 10 2012, 23:24:47)[MSC v.1500 64 bit (AMD64)] IPython 0.13.1

Then I moved the script to another machine (COMP2): Microsoft Windows XP Professional Version 2002 Service Pack 3, AMD Athlon(tm) II X4 620 Processor 2.61 GHz, 3.25 GB of RAM, Physical Address Extension Python 2.7.5 |32 bit | (default, Jun 14 2013,18:15:12) [MSC v.1500 32 bit (Intel)] Ipython 1.0.dev

And the fits are really bad. My code was rather long but I prepared self containing example which reproduces all situation.

I read that sometimes helps when xdata and ydata are array( , dtype=float) but this is not my case (I've tried)

I've even tried this code with Python 2.7.3 32bit on Windows 7 32bit ( my firends' machine) and it worked - so I have no idea why fit results are so unpredictable and how I can force this code working properly on computer with COMP2.

Here is self containing example:

from string import*
from numpy import *
from matplotlib.pylab import *
from scipy.optimize import curve_fit
from sys import exit

nm_range=[574.14200000000005, 574.154, 574.16499999999996, 574.17700000000002, 574.18799999999999, 574.19899999999996, 574.21100000000001, 574.22199999999998, 574.23400000000004, 574.245]
data_for_fit=[859.0, 997.0, 1699.0, 2604.0, 2013.0, 1964.0, 2435.0, 1550.0, 949.0, 841.0]
guess=[574.1861428571428, 574.2155714285715, 1302.0, 1302.0, 0.0035019999999983615, 859.0]

def f_double_gauss(x,x0,x1,A0,A1,sigma,c):
        return A0*exp(-(x-x0)**2/(2.*sigma**2)) + A1*exp(-(x-x1)**2/(2.*sigma**2)) + c

popt,pcov=curve_fit(f_double_gauss,nm_range,data_for_fit,guess,maxfev=10000)

print guess
print popt

fig=figure("If fit of gauss or double gauss is good")
ax=fig.add_subplot(1,1,1)
pdata,=plot(nm_range,data_for_fit,"bo-")
guessed=[]
for i in nm_range:
    guessed.append(f_double_gauss(i,guess[0],guess[1],guess[2],guess[3],guess[4],guess[5]))


pfit,=plot(nm_range,f_double_gauss(nm_range,popt[0],popt[1],popt[2],popt[3],popt[4],popt[5]),"k-")
pguess,=plot(nm_range,guessed,"y")
ax.set_title("Anizo fit"+" : data, init guess & fit")
ax.set_xlabel("wavelenght [nm]")
ax.set_ylabel("PL intensity")
legend([pdata,pguess,pfit],["data","guess","fit"])
show()

Output for bad fit:

[574.1861428571428,574.2155714285715,1302,1302.0,0.0035019999999983615,859.0]
[5.69174152e+02 8.66516577e+04  -9.27629569e+04 1.59887720e+09  7.56288801e-03  1.59110000e+03]

Output for good fit:

[574.1861428571428,574.2155714285715,1302,1302.0,0.0035019999999983615,859.0]
[ 5.74177150e+02    5.74209188e+02  1.74187044e+03  1.58646166e+03  1.0068462e-02   8.57450661e+02]

good fit image: https://docs.google.com/file/d/0B6GA05-W4ZzzdTIxa3U3Rl92MU0/edit?usp=sharing

bad fit image https://docs.google.com/file/d/0B6GA05-W4ZzzRlk4eWlER01WejQ/edit?usp=sharing

Upvotes: 1

Views: 916

Answers (1)

Aleksander_B
Aleksander_B

Reputation: 163

Ok, the problem is with optimize library attached to current scipy.

When I copied _minpack.pyd and minpack.py from files attached to EPD 7.3-2 and put instead of current _minpack.pyd and minpack.py files the fit is perfect.

I will report that bug to scipy.

Upvotes: 3

Related Questions