当前位置:网站首页>Matplotlib-实现常见概率分布
Matplotlib-实现常见概率分布
2022-07-23 05:48:00 【凌贤文】
本次实验所用的4种常见分布,连续分布的代表:beta分布、正态分布,均匀分布,离散分布的代表:二项分布
1、导入模块
import numpy as np
from scipy.stats import beta, norm, uniform, binom
import matplotlib.pyplot as plt
from functools import wraps2、定义带四个参数的画图装饰器
绘图装饰器带有四个参数分别表示legend的2类说明文字,y轴label, 保存的png文件名称。
# 定义带四个参数的画图装饰器
def my_plot(label0=None,
label1=None,
ylabel='probability density function',
fn=None):
def decorate(f):
@wraps(f)
def myplot():
fig = plt.figure(figsize=(16, 9))
ax = fig.add_subplot(111)
x, y, y1 = f()
ax.plot(x, y, linewidth=2, c='r', label=label0)
ax.plot(x, y1, linewidth=2, c='b', label=label1)
ax.legend()
plt.ylabel(ylabel)
plt.show()
plt.savefig('img/%s' % (fn, ))
plt.close()
return myplot
return decorate3、均匀分布(uniform)
从图中可看出,红色概率密度函数只在0~1才会发生,曲线与x轴的0~1区间所封闭的面积为全概率1.0.
@my_plot(label0='b-a=1.0', label1='b-a=2.0', fn='uniform.png')
def unif():
x = np.arange(-0.01, 2.01, 0.01)
y = uniform.pdf(x, loc=0.0, scale=1.0)
y1 = uniform.pdf(x, loc=0.0, scale=2.0)
return x, y, y1
4、二项分布
红色曲线表示发生一次概率为0.3,重复50次的密度函数,二项分布期望值为0.3*50 = 15次。看到这50次实验,很可能出现的次数为10~20.可与蓝色曲线对比分析。
@my_plot(
label0='n=50,p=0.3',
label1='n=50,p=0.7',
fn='binom.png',
ylabel='probability mass function')
def bino():
x = np.arange(50)
n, p, p1 = 50, 0.3, 0.7
y = binom.pmf(x, n=n, p=p)
y1 = binom.pmf(x, n=n, p=p1)
return x, y, y1
5、高斯分布
红色曲线表示均值为0,标准差为1.0的概率密度函数,蓝色曲线的标准差更大,所以它更矮胖,显示出取值的多样性,和不稳定性。
@my_plot(label0='u=0.,sigma=1.0', label1='u=0.,sigma=2.0', fn='guass.png')
def guass():
x = np.arange(-5, 5, 0.1)
y = norm.pdf(x, loc=0.0, scale=1.0)
y1 = norm.pdf(x, loc=0., scale=2.0)
return x, y, y1
6、beta分布
@my_plot(label0='a=10., b=30.', label1='a=4., b=4.', fn='beta.png')
def bet():
x = np.arange(-0.1, 1, 0.001)
y = beta.pdf(x, a=10., b=30.)
y1 = beta.pdf(x, a=4., b=4.)
return x, y, y1
7、绘制所有四种分布
distrs = [unif, bino, guass, bet]
for distri in distrs:
distri()部分代码来源于公众号:Python与算法社区
边栏推荐
- Jenkins deployment
- Query the cross compiled executable dependency Library
- 在GPU上运行MATLAB程序
- 4D毫米波雷达硬件系统架构
- Telnet 配置实例学习记录
- RHCSA--文件内容浏览、cut、uniq、sort、.tr命令使用
- Telnet configuration instance learning record
- Routing and switching technology - static routing
- Rhcsa -- file content browsing, cut, uniq, sort,.Tr command use
- TI单芯片毫米波雷达xWR1642硬件架构研究
猜你喜欢
随机推荐
VLAN的划分以及通过DHCP给所有主机自动分配IP,以及通信全网可达
yum安装LNMP服务部署
vlan配置实例学习记录
Eth-Trunk 配置实例学习记录
Gameframework: package resources, publish packages with the app, package and generate folder instructions, upload resources to the server, download resources, gamefreamworklist DAT and gameframeworkve
Telnet configuration instance learning record
Leetcode problem solution summary
Unity3d: script execution sequence on scene loading gameobejct
0 backtracking / dynamic programming medium leetcode526. Beautiful arrangement
Learning diary - (routing and switching technology) DHCP (Dynamic Host Configuration Protocol)
ACL访问控制实验
制作本地apt源离线安装
HCIA----02
Routing and interface technology -- Summary of direct network
Learning diary - (routing and switching technology) OSPF Protocol
FTP configuration instance learning record
app编译打包部署手册
Routing extension configuration of OSPF and rip
0 shortest path problem leetcode743. Network delay time
0 dynamic programming leetcodde313. super ugly number









