python如何生成各种随机分布图


Posted in Python onAugust 27, 2018

在学习生活中,我们经常性的发现有很多事物背后都有某种规律,而且,这种规律可能符合某种随机分布,比如:正态分布、对数正态分布、beta分布等等。

所以,了解某种分布对一些事物有更加深入的理解并能清楚的阐释事物的规律性。现在,用python产生一组随机数据,来演示这些分布:

import random
import matplotlib
import matplotlib.pyplot as plt
SAMPLE_SIZE = 1000
buckets = 100
fig = plt.figure()
matplotlib.rcParams.update({"font.size": 7})
#第一个图形是在[0,1)之间分布的随机变量(normal distributed random variable)。
ax = fig.add_subplot(5,2,1)
ax.set_xlabel("random.random")
res = [random.random() for _ in xrange(1, SAMPLE_SIZE)]
ax.hist(res, buckets)
#第二个图形是一个均匀分布的随机变量(uniformly distributed random variable)。
ax_2 = fig.add_subplot(5,2,2)
ax_2.set_xlabel("random.uniform")
a = 1
b = SAMPLE_SIZE
res_2 = [random.uniform(a, b) for _ in xrange(1, SAMPLE_SIZE)]
ax_2.hist(res_2, buckets)
#第三个图形是一个三角形分布(triangular distribution)。
ax_3 = fig.add_subplot(5,2,3)
ax_3.set_xlabel("random.triangular")
low = 1
high = SAMPLE_SIZE
res_3 = [random.uniform(low, high) for _ in xrange(1, SAMPLE_SIZE)]
ax_3.hist(res_3, buckets)
#第四个图形是一个beta分布(beta distribution)。参数的条件是alpha 和 beta 都要大于0, 返回值在0~1之间。
plt.subplot(5,2,4)
plt.xlabel("random.betavariate")
alpha = 1
beta = 10
res_4 = [random.betavariate(alpha, beta) for _ in xrange(1, SAMPLE_SIZE)]
plt.hist(res_4, buckets)
#第五个图形是一个指数分布(exponential distribution)。 lambd 的值是 1.0 除以期望的中值,是一个不为零的数(参数应该叫做lambda没但它是python的一个保留字)。如果lambd是整数,返回值的范围是零到正无穷大;如果lambd为负,返回值的范围是负无穷大到零。
plt.subplot(5,2,5)
plt.xlabel("random.expovariate")
lambd = 1.0/ ((SAMPLE_SIZE + 1) / 2.)
res_5 = [random.expovariate(lambd) for _ in xrange(1, SAMPLE_SIZE)]
plt.hist(res_5, buckets)
#第六个图形是gamma分布(gamma distribution), 要求参数alpha 和beta都大于零。
plt.subplot(5,2,6)
plt.xlabel("random.gammavariate")
alpha = 1
beta = 10
res_6 = [random.gammavariate(alpha, beta) for _ in xrange(1, SAMPLE_SIZE)]
plt.hist(res_6, buckets)
#第七个图形是对数正态分布(Log normal distribution)。如果取这个分布的自然对数,会得到一个中值为mu,标准差为sigma的正态分布。mu可以取任何值,sigma必须大于零。
plt.subplot(5,2,7)
plt.xlabel("random.lognormalvariate")
mu = 1
sigma = 0.5
res_7 = [random.lognormvariate(mu, sigma) for _ in xrange(1, SAMPLE_SIZE)]
plt.hist(res_7, buckets)
#第八个图形是正态分布(normal distribution)。
plt.subplot(5,2,8)
plt.xlabel("random.normalvariate")
mu = 1
sigma = 0.5
res_8 = [random.normalvariate(mu, sigma) for _ in xrange(1, SAMPLE_SIZE)]
plt.hist(res_8, buckets)
 
#最后一个图形是帕累托分布(Pareto distribution), alpha 是形状参数。
plt.subplot(5,2,9)
plt.xlabel("random.normalvariate")
alpha = 1
res_9 = [random.paretovariate(alpha) for _ in xrange(1, SAMPLE_SIZE)]
plt.hist(res_9, buckets)
plt.show()

python如何生成各种随机分布图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python判断变量是否已经定义的方法
Aug 18 Python
轻松实现python搭建微信公众平台
Feb 16 Python
浅析python中SQLAlchemy排序的一个坑
Feb 24 Python
pandas 转换成行列表进行读取与Nan处理的方法
Oct 30 Python
Python2 Selenium元素定位的实现(8种)
Feb 25 Python
python爬虫selenium和phantomJs使用方法解析
Aug 08 Python
Python使用urllib模块对URL网址中的中文编码与解码实例详解
Feb 18 Python
Pandas —— resample()重采样和asfreq()频度转换方式
Feb 26 Python
Python 实现打印单词的菱形字符图案
Apr 12 Python
Jupyter notebook如何修改平台字体
May 13 Python
python中adb有什么功能
Jun 07 Python
python输入中文的实例方法
Sep 14 Python
python随机数分布random测试
Aug 27 #Python
pycharm安装和首次使用教程
Aug 27 #Python
Windows下PyCharm安装图文教程
Aug 27 #Python
python 3.7.0 安装配置方法图文教程
Aug 27 #Python
python 3.7.0 下pillow安装方法
Aug 27 #Python
python3.7.0的安装步骤
Aug 27 #Python
利用Django-environ如何区分不同环境
Aug 26 #Python
You might like
让你的网站首页自动选择语言转跳
2006/12/06 PHP
使用PHP备份MYSQL数据的多种方法
2014/01/15 PHP
CodeIgniter删除和设置Cookie的方法
2015/04/07 PHP
php实现的网络相册图片防盗链完美破解方法
2015/07/01 PHP
thinkPHP+ajax实现统计页面pv浏览量的方法
2017/03/15 PHP
总结PHP代码规范、流程规范、git规范
2018/06/18 PHP
调试Node.JS的辅助工具(NodeWatcher)
2012/01/04 Javascript
javascript实现促销倒计时+fixed固定在底部
2013/09/18 Javascript
jQuery实现动画效果的简单实例
2014/01/27 Javascript
js获取页面传来参数的方法
2014/09/06 Javascript
JavaScript随机生成信用卡卡号的方法
2015/04/07 Javascript
JS实现弹性漂浮效果的广告代码
2015/09/02 Javascript
将JavaScript的jQuery库中表单转化为JSON对象的方法
2015/11/17 Javascript
jQuery-1.9.1源码分析系列(十一)DOM操作续之克隆节点
2015/12/01 Javascript
使用jquery获取url以及jquery获取url参数的实现方法
2016/05/25 Javascript
浅谈在js传递参数中含加号(+)的处理方式
2016/10/11 Javascript
基于jQuery实现文字打印动态效果
2017/04/21 jQuery
最常用的jQuery表单验证(简单)
2017/05/23 jQuery
js中json对象和字符串的理解及相互转化操作实现方法
2017/09/22 Javascript
vue项目中使用tinymce编辑器的步骤详解
2018/09/11 Javascript
详解jQuery中的getAll()和cleanData()
2019/04/15 jQuery
js原生map实现的方法总结
2020/01/19 Javascript
微信小程序间使用navigator跳转传值问题实例分析
2020/03/27 Javascript
python处理PHP数组文本文件实例
2014/09/18 Python
利用python获取某年中每个月的第一天和最后一天
2016/12/15 Python
解决Python安装后pip不能用的问题
2018/06/12 Python
TensorFlow实现从txt文件读取数据
2020/02/05 Python
Python响应对象text属性乱码解决方案
2020/03/31 Python
在python3.64中安装pyinstaller库的方法步骤
2020/06/02 Python
外贸英语专业求职信范文
2013/12/25 职场文书
初中考试作弊检讨书
2014/02/01 职场文书
教代会闭幕词
2015/01/28 职场文书
面试复试通知单
2015/04/24 职场文书
学校光盘行动倡议书
2015/04/28 职场文书
2016国庆促销广告语
2016/01/28 职场文书
最新的离婚协议书范本!
2019/07/02 职场文书