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的Flask框架中的Jinja2模板引擎学习教程
Jun 30 Python
python判断字符串或者集合是否为空的实例
Jan 23 Python
python实现串口自动触发工作的示例
Jul 02 Python
Python循环实现n的全排列功能
Sep 16 Python
Python大数据之网络爬虫的post请求、get请求区别实例分析
Nov 16 Python
Python Pickle 实现在同一个文件中序列化多个对象
Dec 30 Python
python 实现线程之间的通信示例
Feb 14 Python
基于Python中random.sample()的替代方案
May 23 Python
Python matplotlib模块及柱状图用法解析
Aug 10 Python
python利用paramiko实现交换机巡检的示例
Sep 22 Python
浅析Python中字符串的intern机制
Oct 03 Python
如何用Python 实现全连接神经网络(Multi-layer Perceptron)
Oct 15 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 date函数参数详解
2006/11/27 PHP
克隆一个新项目的快捷方式
2013/04/10 PHP
PHP实现一个限制实例化次数的类示例
2019/09/16 PHP
Thinkphp 框架扩展之Widget扩展实现方法分析
2020/04/23 PHP
PHP接口类(interface)的定义、特点和应用示例
2020/05/18 PHP
JS面向对象编程 for Cookie
2010/09/19 Javascript
客户端限制只能上传jpg格式图片的js代码
2010/12/09 Javascript
js里取容器大小、定位、距离等属性搜集整理
2013/08/19 Javascript
jquery判断浏览器后退时候弹出消息的方法
2014/08/11 Javascript
jQuery与Ajax以及序列化
2016/02/01 Javascript
微信小程序 form组件详解及简单实例
2017/01/10 Javascript
javascript获取以及设置光标位置
2017/02/16 Javascript
ES6新特性之变量和字符串用法示例
2017/04/01 Javascript
纯js实现的积木(div层)拖动功能示例
2017/07/19 Javascript
Node.js readline模块与util模块的使用
2018/03/01 Javascript
如何以Angular的姿势打开Font-Awesome详解
2018/04/22 Javascript
layDate日期控件使用方法详解
2018/11/15 Javascript
JavaScript实现简单随机点名器
2019/11/21 Javascript
原生js实现俄罗斯方块
2020/10/20 Javascript
[36:41]完美世界DOTA2联赛循环赛FTD vs Magma第一场 10月30日
2020/10/31 DOTA
Python 文件重命名工具代码
2009/07/26 Python
在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
2015/04/09 Python
使用Python发送邮件附件以定时备份MySQL的教程
2015/04/25 Python
从Python的源码来解析Python下的freeblock
2015/05/11 Python
Python判断直线和矩形是否相交的方法
2015/07/14 Python
python实现读取excel写入mysql的小工具详解
2017/11/20 Python
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
2018/02/05 Python
tensorflow 报错unitialized value的解决方法
2020/02/06 Python
python分别打包出32位和64位应用程序
2020/02/18 Python
解决安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示no Qt platform plugin的问题
2020/04/24 Python
Skyscanner阿联酋:全球领先的旅游搜索平台
2017/11/25 全球购物
WoolOvers澳洲官方网站:英国针织服装公司
2018/05/13 全球购物
法国购买隐形眼镜和眼镜网站:Optical Center
2019/10/08 全球购物
建筑工程造价专业自荐信
2014/07/08 职场文书
合作协议书格式
2014/08/19 职场文书
python三子棋游戏
2022/05/04 Python