product baskets scaling_factor12345 475 95.512345 108 57.712345 2 1.412345 38 21.912345 320 88.8
我想运行以下非线性回归并估计参数.
a,b和c
我想要适合的等式:
scaling_factor = a - (b*np.exp(c*baskets))
在sas中我们通常运行以下模型:(使用高斯牛顿法)
proc nlin data=scaling_factors; parms a=100 b=100 c=-0.09; model scaling_factor = a - (b * (exp(c*baskets))); output out=scaling_equation_parms parms=a b c;
有没有类似的方法来估计Python中的参数使用非线性回归,我怎么能看到python中的情节.
解决方法 同意Chris Mueller,我也会使用scipy而不是scipy.optimize.curve_fit
. 代码如下:
###the top two lines are required on my linux machineimport matplotlibmatplotlib.use('Qt4Agg')import matplotlib.pyplot as pltfrom matplotlib.pyplot import cmimport numpy as npfrom scipy.optimize import curve_fit #we Could import more,but this is what we need###defining your fitfunctiondef func(x,a,b,c): return a - b* np.exp(c * x) ###OP's databaskets = np.array([475,108,2,38,320])scaling_factor = np.array([95.5,57.7,1.4,21.9,88.8])###let us guess some start valuesinitialGuess=[100,100,-.01]guessedFactors=[func(x,*initialGuess ) for x in baskets]###making the actual fitpopt,pcov = curve_fit(func,baskets,scaling_factor,initialGuess)#one may want toprint poptprint pcov###preparing data for showing the fitbasketCont=np.linspace(min(baskets),max(baskets),50)fittedData=[func(x,*popt) for x in basketCont]###preparing the figurefig1 = plt.figure(1)ax=fig1.add_subplot(1,1,1)###the three sets of data to plotax.plot(baskets,linestyle='',marker='o',color='r',label="data")ax.plot(baskets,guessedFactors,marker='^',color='b',label="initial guess")ax.plot(basketCont,fittedData,linestyle='-',color='#900000',label="fit with ({0:0.2g},{1:0.2g},{2:0.2g})".format(*popt))###beautificationax.legend(loc=0,title="graphs",Fontsize=12)ax.set_ylabel("factor")ax.set_xlabel("baskets")ax.grID()ax.set_Title("$\mathrm{curve}_\mathrm{fit}$")###putting the covariance matrix nicelytab= [['{:.2g}'.format(j) for j in i] for i in pcov]the_table = plt.table(cellText=tab,colWIDths = [0.2]*3,loc='upper right',bBox=[0.483,0.35,0.5,0.25] )plt.text(250,65,'covariance:',size=12)###putting the plotplt.show()###done
最后,给你:
以上是内存溢出为你收集整理的如何在python中运行非线性回归全部内容,希望文章能够帮你解决如何在python中运行非线性回归所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)