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 不关闭控制台的实现方法
Oct 23 Python
Python修改Excel数据的实例代码
Nov 01 Python
python更改已存在excel文件的方法
May 03 Python
Python处理菜单消息操作示例【基于win32ui模块】
May 09 Python
django之跨表查询及添加记录的示例代码
Oct 16 Python
Python正则表达式和re库知识点总结
Feb 11 Python
对python中的os.getpid()和os.fork()函数详解
Aug 08 Python
Python高阶函数、常用内置函数用法实例分析
Dec 26 Python
vue学习笔记之动态组件和v-once指令简单示例
Feb 29 Python
python自动统计zabbix系统监控覆盖率的示例代码
Apr 03 Python
Python实现Telnet自动连接检测密码的示例
Apr 16 Python
详解Go语言运用广度优先搜索走迷宫
Jun 23 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中ADODB类详解
2008/03/25 PHP
PHP的加密方式及原理
2012/06/14 PHP
PHP之短标签开启设置
2013/06/17 PHP
CodeIgniter基本配置详细介绍
2013/11/12 PHP
javascript编程起步(第二课)
2007/01/10 Javascript
jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable示例代码打包
2011/04/06 Javascript
jQuery EasyUI API 中文文档 - Calendar日历使用
2011/10/19 Javascript
jquery+ajax实现跨域请求的方法
2015/01/20 Javascript
Jquery Ajax xmlhttp请求成功问题
2015/02/04 Javascript
基于javascript实现图片预加载
2016/01/05 Javascript
Angularjs中UI Router全攻略
2016/01/29 Javascript
微信小程序 开发指南详解
2016/09/27 Javascript
ReactNative-JS 调用原生方法实例代码
2016/10/08 Javascript
Javascript中常用的检测方法小结
2016/10/08 Javascript
JavaScript中数据类型转换总结
2016/12/25 Javascript
B/S(Web)实时通讯解决方案分享
2017/04/06 Javascript
layui实现动态和静态分页
2018/04/28 Javascript
autojs 蚂蚁森林能量自动拾取即给指定好友浇水的实现方法
2020/05/03 Javascript
如何基于jQuery实现五角星评分
2020/09/02 jQuery
原生JavaScript实现刮刮乐
2020/09/29 Javascript
如何利用JavaScript编写一个格斗小游戏
2021/01/06 Javascript
Python中encode()方法的使用简介
2015/05/18 Python
python处理Excel xlrd的简单使用
2017/09/12 Python
Python实现的NN神经网络算法完整示例
2018/06/19 Python
Python中如何使用if语句处理列表实例代码
2019/02/24 Python
在Python IDLE 下调用anaconda中的库教程
2020/03/09 Python
删除pycharm鼠标右键快捷键打开项目的操作
2021/01/16 Python
法学毕业生自我鉴定
2014/01/31 职场文书
优秀部门获奖感言
2014/02/14 职场文书
2014大学生中国梦主题教育学习思想汇报
2014/09/10 职场文书
县级领导干部开展党的群众路线教育实践活动工作汇报
2014/10/25 职场文书
保护环境建议书作文400字
2015/09/14 职场文书
go语言求任意类型切片的长度操作
2021/04/26 Golang
了解Redis常见应用场景
2021/06/23 Redis
青岛市的收音机研制与生产
2022/04/07 无线电
macos系统如何实现微信双开? mac登录两个微信以上微信的技巧
2022/07/23 数码科技