本文介绍了scipy.optimize curve_fit返回错误的值(取决于机器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用scipy.optimize curve_fit编写了一些代码。它可以在我的计算机上完美运行:

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

带有Service Pack 1的Windows 7 Home Premium,64位
Dell Studio 1558
Intel Core i3 cpu [email protected] GHz 2.13GHz,3.86 GB RAM
Python 2.7.3(默认值,2012年4月10日,23:24:47)[MSC v.1500 64位(AMD64)]
IPython 0.13.1

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

然后我将脚本移动到另一台计算机(COMP2):
Microsoft Windows XP Professional版本2002 Service Pack 3,AMD Athlon(tm)II X4 620
处理器2.61 GHz,3.25 GB RAM,物理地址扩展
Python 2.7.5 | 32位| (默认值,2013年6月14日,18:15:12)[MSC v.1500 32位(英特尔)]
Ipython 1.0.dev

Then I moved the script to another machine (COMP2):Microsoft Windows XP Professional Version 2002 Service Pack 3, AMD Athlon(tm) II X4 620Processor 2.61 GHz, 3.25 GB of RAM, Physical Address ExtensionPython 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.

我读到有时xdata和ydata是array(,dtype = float)会有所帮助,但这不是我的情况(我已经尝试过)

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

我什至在Windows 7 32bit(我的朋友的机器)上使用Python 2.7.3 32bit尝试了此代码,有效-我不知道为什么拟合结果如此不可预测,以及如何强制此代码在具有COMP2的计算机上正常工作。

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.

此处是自包含的示例:

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()

不良匹配的输出:

[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]

输出良好适合:

[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]

好身材图片:

不良图片

推荐答案

好的,问题出在与当前scipy相连的优化库上。

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

当我复制 _minpack.pyd minpack.py 从附加到EPD 7.3-2的文件中,并代替当前的 _minpack.pyd minpack.py 文件适合。

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.

我将将该错误报告给scipy。

I will report that bug to scipy.

这篇关于scipy.optimize curve_fit返回错误的值(取决于机器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:30