我想问一个关于用户对其他问题的回复的问题,但是由于某种原因,评论框没有出现。对不起,如果我做错了什么。在
无论如何,关于这个答复: https://stackoverflow.com/a/11507723/1950164
我有以下问题:如何使用此代码将不同的数据适应不同的函数?我有一个和他解决的问题相似的问题,希望我能拟合累积分布。所以我开始试着概括代码。我做了三个修改:
a)在计算直方图的行之后,我添加了
hist = numpy.cumsum(hist)
这将我们的分布转化为累积分布
b)我定义了一个新函数,而不是示例中的高斯函数
^{pr2}$这就是高斯分布的累积值。在
c)最后,当然,我更改了曲线拟合线来调用我的函数:
coeff, var_matrix = curve_fit(myerf, bin_centres, hist, p0=p0)
这应该是一个微不足道的练习,除非它不起作用。程序现在返回以下错误消息:
bash-3.2$ python fitting.py
Traceback (most recent call last):
File "fitting.py", line 27, in <module>
coeff, var_matrix = curve_fit(myerf, bin_centres, hist, p0=p0)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 506, in curve_fit
res = leastsq(func, p0, args=args, full_output=1, **kw)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 348, in leastsq
m = _check_func('leastsq', 'func', func, x0, args, n)[0]
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 14, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 418, in _general_function
return function(xdata, *params) - ydata
File "fitting.py", line 22, in myerf
return A/2. * (1+math.erf((x-mu)/(math.sqrt(2)*sigma)))
TypeError: only length-1 arrays can be converted to Python scalars
我做错什么了?在
另外:给我一个参考,它解释了函数的自变量中,*p是什么。在
谢谢!在
编辑:我试着用累积分布数据运行程序,但仍然调用高斯函数。这很管用,你只是不太适合。所以错误应该出在myerf功能的某个地方。在
编辑2:如果我试着用更简单的方法替换myerf函数的返回值,比如
return A + mu*x + sigma*x**2
那就行了。所以在回报中一定有一些东西没有做它应该做的。在
EDIT3:所以,我试着用scipy的error函数代替math中的error函数,现在可以用了。我不知道为什么它以前不起作用,但现在起作用了。所以代码是:
import matplotlib
matplotlib.use('Agg')
import numpy, math
import pylab as pl
from scipy.optimize import curve_fit
from scipy.special import erf
# Define some test data which is close to Gaussian
data = numpy.random.normal(size=10000)
hist, bin_edges = numpy.histogram(data, density=True)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2
hist = numpy.cumsum(hist)
def myerf(x, *p):
A, mu, sigma = p
return A/2. * ( 1+erf(((x-mu)/(math.sqrt(2)*sigma))) )
# p0 is the initial guess for the fitting coefficients (A, mu and sigma above)
p0 = [1., 0., 1.]
coeff, var_matrix = curve_fit(myerf, bin_centres, hist, p0=p0)
# Get the fitted curve
hist_fit = myerf(bin_centres, *coeff)
pl.plot(bin_centres, hist, label='Test data')
pl.plot(bin_centres, hist_fit, label='Fitted data')
# Finally, lets get the fitting parameters, i.e. the mean and standard deviation:
print 'Fitted mean = ', coeff[1]
print 'Fitted standard deviation = ', coeff[2]
pl.savefig('fitting.png')
pl.show()
与
math
函数不同,numpy
函数接受向量输入:相关问题 更多 >
编程相关推荐