直方图的指数拟合

2024-09-24 22:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在由变量y1_pt创建的直方图上拟合指数曲线,然后得到指数的参数。问题是它给了我以下警告:

优化警告:无法估计参数的协方差 和pcov_指数=

array([[inf, inf, inf],
       [inf, inf, inf],
       [inf, inf, inf]]))

结果更像是指数拟合,在我看来有点随机。。(见图表) 有人知道怎么回事吗


import pandas as pd
import numpy
from pylab import *
import scipy.stats as ss
from scipy.optimize import curve_fit


df=pd.read_hdf('data.h5','dataset')

pty1 = df1['y1_pt']

bins1 = numpy.linspace(35, 1235, 100)
counts, bins = numpy.histogram(pty1, bins = bins1, range = [35, 1235], density = False)
binscenters = numpy.array([0.5 * (bins1[i] + bins1[i+1]) for i in range(len(bins1)-1)])
def exponential(x, a, k, b):
    return a*np.exp(-x*k) + b


popt_exponential, pcov_exponential = curve_fit(exponential,  xdata=binscenters, ydata=counts)
print(popt_exponential)

xspace = numpy.linspace(0, 6, 100000)
plt.bar(binscenters, counts, color='navy', label=r'Histogram entries')
plt.plot(xspace, exponential(xspace, *popt_exponential), color='darkorange', linewidth=2.5, label=r'Fitted function')
plt.show()

enter image description here


Tags: importnumpypt警告参数plt指数inf