您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python:从经验分布中生成随机值

5b51 2022/1/14 8:22:02 python 字数 2869 阅读 518 来源 www.jb51.cc/python

在Java中,我通常依赖org.apache.commons.math3.random.EmpiricalDistribution类来执行以下操作:>从观察到的数据中导出概率分布.>从此分布生成随机值.有没有提供相同功能的Python库?似乎scipy.stats.gaussian_kde.resample做了类似的事情,但我不确定它是否实现了与

概述

Java中,我通常依赖org.apache.commons.math3.random.EmpiricalDistribution类来执行以下操作:

>从观察到的数据中导出概率分布.
>从此分布生成随机值.

有没有提供相同功能的Python库?似乎scipy.stats.gaussian_kde.resample做了类似的事情,但我不确定它是否实现了与我熟悉的java类型相同的过程.

import numpy as np
import scipy.stats
import matplotlib.pyplot as plt

# This represents the original "empirical" sample -- I fake it by
# sampling from a normal distribution
orig_sample_data = np.random.normal(size=10000)

# Generate a KDE from the empirical sample
sample_pdf = scipy.stats.gaussian_kde(orig_sample_data)

# Sample new datapoints from the KDE
new_sample_data = sample_pdf.resample(10000).T[:,0]

# Histogram of initial empirical sample
cnts,bins,p = plt.hist(orig_sample_data,label='original sample',bins=100,histtype='step',linewidth=1.5,density=True)

# Histogram of datapoints sampled from KDE
plt.hist(new_sample_data,label='sample from KDE',bins=bins,density=True)

# Visualize the kde itself
y_kde = sample_pdf(bins)
plt.plot(bins,y_kde,label='KDE')
plt.legend()
plt.show(block=False)

resulting plot

new_sample_data应该从与原始数据大致相同的分布中绘制(达到KDE与原始分布的良好近似程度).

总结

以上是编程之家为你收集整理的Python:从经验分布中生成随机值全部内容,希望文章能够帮你解决Python:从经验分布中生成随机值所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶