如何用对数正态分布函数拟合数据

2024-10-02 22:26:15 发布

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

我是Python的新用户,我正在尝试将一些实验数据与CDF相匹配。这些数据如下所示,它们应该以x轴对数比例绘制:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x=np.array([0.995, 3.003, 5.908, 10.525, 13.617, 24.321, 33.917, 47.843, 64.172, 91.353, 126.745, 174.118, 225.059, 292.998, 369.133, 640.295, 828.169, 1255.39, 1496.613, 1942.785])

y=np.array([0.142, 0.2, 0.25, 0.36, 0.498, 0.616, 0.599, 0.7, 0.835, 1.102, 1.083, 1.225, 1.133, 1.165, 1.298, 1.365, 1.298, 1.373, 1.409, 1.538])

pyplot.xscale('log')

plt.plot(x,y,'r.')

我发现了一个来自同一用户的证词,该证词使用以下方法拟合了数据:

^{pr2}$

enter image description here

但是,当我试图将这段代码适应我自己的问题(上面显示了数据),这个问题处理的是对数正态分布pdf和cdf,结果并不理想。在

enter image description here

如果有人能帮我,我会非常感激的!在

提前谢谢


Tags: 数据用户imageimportherematplotlibasnp
1条回答
网友
1楼 · 发布于 2024-10-02 22:26:15

您可以通过使用scipy.optimize.curve_fit并定义log函数来拟合y=a*log10(b*x+h)+k(这似乎比对数正态分布更适合您的数据)。在

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import lognorm
from scipy.optimize import curve_fit

x=np.array([0.995, 3.003, 5.908, 10.525, 13.617, 24.321, 33.917, 47.843, 64.172, 91.353, 126.745, 174.118, 225.059, 292.998, 369.133, 640.295, 828.169, 1255.39, 1496.613, 1942.785])

y=np.array([0.142, 0.2, 0.25, 0.36, 0.498, 0.616, 0.599, 0.7, 0.835, 1.102, 1.083, 1.225, 1.133, 1.165, 1.298, 1.365, 1.298, 1.373, 1.409, 1.538])

def log(x, a, b, h, k):
    return a*np.log10(b*x + h) + k

# Provide guesses to the parameters
params = [6, 1, 0, 0]#, .2]
popt, pcov = curve_fit(log, x, y, p0=params)

plt.plot(x,y,'r.')
plt.plot(x, log(x, *popt), 'k ', label = 'fit: a = %.2f b = %.2f h = %.2f k = %.2f ' 
% tuple(popt)) #locl = %.2f scale = %.2f
plt.legend(loc = 'lower right')
#plt.xscale('log')
plt.show()

如果没有原木秤,你会得到像

enter image description here

用原木秤,你的身材看起来像

enter image description here

相关问题 更多 >