python正态分布拟合曲线怎么打印出中位数值_在Python中拟合合并的对数正态数据...

本文介绍了如何处理粒度分布数据,并使用Python的scipy库进行对数正态分布拟合。通过将百分比体积分数的数据转换为变量数组,然后利用`curve_fit`函数进行曲线拟合,从而找到最佳参数。这种方法适用于处理离散的粒度分布数据,并能生成实际和拟合分布的图形对比。
摘要由CSDN通过智能技术生成

I have a range of particle size distribution data arranged by percentage volume fraction, like so:;

size %

6.68 0.05

9.92 1.15

etc.

I need to fit this data to a lognormal distribution, which I planned to do using python's stats.lognorm.fit function, but this seems to expect the input as an array of variates rather than binned data, judging by what I've read.

I was planning to use a for loop to iterate through the data and .extend each size entry to a placeholder array the required number of times to create an array with a list of variates that corresponds to the binned data.

This seems really ugly and inefficient though, and the kind of thing that there's probably an easy way to do. Is there a way to input binned data into the stats.lognorm.fit function?

解决方案

I guess one possible workaround is to manually fit a pdf to your bin data, assuming x values are the midpoint of each interval, and y values are the corresponding bin frequency. And then fit a curve based on x and y values using scipy.optimize.curve_fit. I think accuracy of the results will depend the number of bins you have. An example is shown below:

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

import numpy as np

def pdf(x, mu, sigma):

"""pdf of lognormal distribution"""

return (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))

mu, sigma = 3., 1. # actual parameter value

data = np.random.lognormal(mu, sigma, size=1000) # data generation

h = plt.hist(data, bins=30, normed = True)

y = h[0] # frequencies for each bin, this is y value to fit

xs = h[1] # boundaries for each bin

delta = xs[1] - xs[0] # width of bins

x = xs[:-1] + delta / # midpoints of bins, this is x value to fit

popt, pcov = curve_fit(pdf, x, y, p0=[1, 1]) # data fitting, popt contains the fitted parameters

print(popt)

# [ 3.13048122 1.01360758] fitting results

fig, ax = plt.subplots()

ax.hist(data, bins=30, normed=True, align='mid', label='Histogram')

xr = np.linspace(min(xs), max(xs), 10000)

yr = pdf(xr, mu, sigma)

yf = pdf(xr, *popt)

ax.plot(xr, yr, label="Actual")

ax.plot(xr, yf, linestyle = 'dashed', label="Fitted")

ax.legend()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值