当我试图用抛物线拟合这个数据时,我得到了一个可怕的拟合。 我最初是做一个直方图的数据,这是一个对象的位置,然后绘制负对数值的直方图bin计数的位置使用抛物线拟合。 我使用的代码是:
time,pos=postime()
plt.plot(time, pos)
poslen=len(pos)
plt.xlabel('Time')
plt.ylabel('Positions')
plt.show()
n,bins,patches = plt.hist(pos,bins=100)
n=n.tolist()
plt.show()
l=len(bins)
s=len(n)
posx=[]
i=0
j=0
pbin=[]
sig=[]
while j < (l-1):
pbin.append((bins[j]+bins[j+1])/2)
j=j+1
while i < s:
if n[i]==0:
pbin[i]=0
else:
sig.append(np.power(1/n[i],2))
n[i]=n[i]/poslen
n[i]=np.log(n[i])
n[i]=n[i]*(-1)
i=i+1
n[:]=[y for y in n if y != 0]
pbin[:]=[y for y in pbin if y != 0]
from scipy.optimize import curve_fit
def parabola(x, a , b):
return a * (np.power(x,2)) + b
popt, pcov = curve_fit(parabola, pbin, n)
print popt
plt.plot(pbin,n)
plt.plot(pbin, parabola(pbin, *popt), 'r-')
也可以使用矩阵求逆。你知道吗
矩阵形式是
如果需要的话,你可以有一个更好的主意。它并不总是有效的,您可能需要应用某种形式的regularization。你知道吗
我不知道你为什么要计算直方图。。。但这里有一个不需要直方图计算的工作示例。你知道吗
另外,如果你的
time_
向量不是均匀采样的,你希望它是均匀采样的拟合,你可以做:fittime_ = np.linsapce(np.min(time_), np.max(time_))
然后yfit = parabola(fittime_, *popt)
。你知道吗相关问题 更多 >
编程相关推荐