在 SciPy (Python) 中从拟合 PDF 生成随机样本 [英] Generating random samples from fit PDF in SciPy (Python)

查看:62
本文介绍了在 SciPy (Python) 中从拟合 PDF 生成随机样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 scipy.stats 为数据集提供拟合分布,其内容类似于:

Given a fit distribution to a dataset using scipy.stats with something similar to:

data = fetch_data(file)
x = np.linspace(0, 100, 1000)

param = scipy.stats.norm.fit(data)
fit_pdf = scipy.stats.norm.pdf(x, param[0], param[1])

从这个拟合数据生成 N=1000 个随机样本的最佳方法是什么?给定 PDF 中的任何值数组,是否可以生成随机样本?

What is the best way to generate N=1000 random samples from this fit data? Is it possible to generate random samples given any array of values in a PDF?

推荐答案

生成随机样本的最佳方法是:

The best way to generate the random samples is:

data = fetch_data(file)
x = np.linspace(0, 100, 1000)

param = scipy.stats.norm.fit(data)
random_samples = scipy.stats.norm.rvs(param[0], param[1], size=1000)

要使用给定的 pdf 作为数组生成随机样本,您可以使用以下命令:

To generate random samples using a given pdf as an array you can use the following:

fit_pdf = scipy.stats.norm.pdf(x, param[0], param[1])

samples = np.random.choice(x, size=1000, p=fit_pdf/np.sum(fit_pdf)) 

这篇关于在 SciPy (Python) 中从拟合 PDF 生成随机样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆