python 非线性回归_如何在python中运行非线性回归

同意Chris Mueller的观点,我也会使用scipy但是^{}。

代码如下:###the top two lines are required on my linux machine

import matplotlib

matplotlib.use('Qt4Agg')

import matplotlib.pyplot as plt

from matplotlib.pyplot import cm

import numpy as np

from scipy.optimize import curve_fit #we could import more, but this is what we need

###defining your fitfunction

def func(x, a, b, c):

return a - b* np.exp(c * x)

###OP's data

baskets = 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 values

initialGuess=[100, 100,-.01]

guessedFactors=[func(x,*initialGuess ) for x in baskets]

###making the actual fit

popt,pcov = curve_fit(func, baskets, scaling_factor,initialGuess)

#one may want to

print popt

print pcov

###preparing data for showing the fit

basketCont=np.linspace(min(baskets),max(baskets),50)

fittedData=[func(x, *popt) for x in basketCont]

###preparing the figure

fig1 = plt.figure(1)

ax=fig1.add_subplot(1,1,1)

###the three sets of data to plot

ax.plot(baskets,scaling_factor,linestyle='',marker='o', color='r',label="data")

ax.plot(baskets,guessedFactors,linestyle='',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))

###beautification

ax.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 nicely

tab= [['{:.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 plot

plt.show()

###done

最后,给你:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值