具有自定义数据集输出的指数曲线拟合是平坦的

2024-04-27 03:05:00 发布

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

我从同事那里取了一些权重,引导了1000次运行,每次运行从原始数据中抽取3个值并附加最小值,现在我尝试用曲线拟合这些值以指数拟合。当我绘制柱状图时,它不是一个很好的指数值,但我认为我做错了什么,因为我只是得到了一条平线。你知道吗

下面的值样本包含1000个样本(每个样本是原始数据中3个样本的最小值)

figure = plt.figure(figsize=(10, 6)) # first element is width, second is height.
axes = figure.add_subplot(1, 1, 1)
#axes.hist( sample, density=True, color="dimgray") # a density
axes.set_ylabel( "Density")
axes.set_xlabel( "Weights")
axes.set_title( "Coworker Weights")

ys, bins = np.histogram(sample,bins = 7)
axes.plot(bins[:-1],ys)

print(bins,ys)

下一张照片的情节很难看,但我仍然认为它应该符合一条曲线:

enter image description here

接下来,我尝试拟合数据:

def func(x, a, b, c):
    return a * np.exp(-b * x) + c

weights = np.array(weights)
popt, pcov = curve_fit(func, bins[:-1], ys)
print(popt)

figure = plt.figure(figsize=(10, 6)) # first element is width, second is height.
axes = figure.add_subplot(1, 1, 1)
plt.plot(weights, func(weights, *popt))

权重数组是我的原始数据。我得到的输出只是一条平线,popt系数是[1,1,143]。我做错什么了?你知道吗


Tags: 原始数据isnpplt权重figurefunc样本