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中吸引人的一些特性
Apr 09 Python
用Python创建声明性迷你语言的教程
Apr 13 Python
Python实现将绝对URL替换成相对URL的方法
Jun 28 Python
Python语言实现获取主机名根据端口杀死进程
Mar 31 Python
用python记录运行pid,并在需要时kill掉它们的实例
Jan 16 Python
Python搭建FTP服务器的方法示例
Jan 19 Python
python爬取cnvd漏洞库信息的实例
Feb 14 Python
树莓派+摄像头实现对移动物体的检测
Jun 22 Python
python 列表转为字典的两个小方法(小结)
Jun 28 Python
python用win32gui遍历窗口并设置窗口位置的方法
Jul 26 Python
Django 允许局域网中的机器访问你的主机操作
May 13 Python
python在地图上画比例的实例详解
Nov 13 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
十天学会php之第二天
2006/10/09 PHP
php mssql 日期出现中文字符的解决方法
2009/03/10 PHP
MayFish PHP的MVC架构的开发框架
2009/08/13 PHP
php 向访客和爬虫显示不同的内容
2009/11/09 PHP
php获取客户端电脑屏幕参数的方法
2015/01/09 PHP
PHP静态延迟绑定和普通静态效率的对比
2017/10/20 PHP
PHP如何解决微信文章图片防盗链
2020/12/09 PHP
给网站上的广告“加速”显示的方法
2007/04/08 Javascript
List Information About the Binary Files Used by an Application
2007/06/18 Javascript
JavaScript中出现乱码的处理心得
2009/12/24 Javascript
JavaScript获得url查询参数的方法
2015/07/02 Javascript
JavaScript入门基础
2015/08/12 Javascript
JavaScript实现的简单烟花特效代码
2015/10/20 Javascript
Node.js用readline模块实现输入输出
2016/12/16 Javascript
使用vue与jquery实时监听用户输入状态的操作代码
2017/09/19 jQuery
jQuery中图片展示插件highslide.js的简单dom
2018/04/22 jQuery
jQuery实现每隔一段时间自动更换样式的方法分析
2018/05/03 jQuery
JS实现获取数组中最大值或最小值功能示例
2019/03/02 Javascript
Vue项目中配置pug解析支持
2019/05/10 Javascript
微信小程序实现页面跳转传递参数(实体,对象)
2019/08/12 Javascript
判断网页编码的方法python版
2016/08/12 Python
Python Queue模块详细介绍及实例
2016/12/27 Python
python json.loads兼容单引号数据的方法
2018/12/19 Python
Python中断多重循环的思路总结
2019/10/04 Python
TensorFlow tensor的拼接实例
2020/01/19 Python
python使用Thread的setDaemon启动后台线程教程
2020/04/25 Python
Python多线程的退出控制实现
2020/08/10 Python
CSS3 transition 实现通知消息轮播条
2020/10/14 HTML / CSS
总经理助理岗位职责
2013/11/08 职场文书
心得体会开头
2014/01/01 职场文书
小学生班会演讲稿
2014/01/09 职场文书
银行委托书范本
2014/04/04 职场文书
离婚协议书怎么写2014
2014/09/30 职场文书
2014年调度员工作总结
2014/11/19 职场文书
幼儿园园长六一致辞
2015/07/31 职场文书
旷工检讨书大全
2015/08/15 职场文书