当前位置:网站首页>Matplotlib- implement common probability distributions
Matplotlib- implement common probability distributions
2022-07-23 13:23:00 【Ling Xianwen】
The... Used in this experiment 4 Species of common distribution , Representative of continuous distribution :beta Distribution 、 Normal distribution , Uniform distribution , Representative of discrete distribution : The binomial distribution
1、 The import module
import numpy as np
from scipy.stats import beta, norm, uniform, binom
import matplotlib.pyplot as plt
from functools import wraps2、 Define a drawing decorator with four parameters
The drawing decorator has four parameters to represent legend Of 2 Class description text ,y Axis label, The saved png File name .
# Define a drawing decorator with four parameters
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 distribution (uniform)
As you can see from the diagram , The red probability density function is only in 0~1 Will happen , Curve and x The shaft 0~1 The area enclosed by the section is Total probability 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、 The binomial distribution
The red curve indicates that the probability of occurrence is 0.3, repeat 50 Density function of degree , The binomial distribution The expected value is 0.3*50 = 15 Time . See this 50 Experiments , The likely number of occurrences is 10~20. It can be compared with the blue curve .
@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、 Gaussian distribution
The red curve indicates that the mean value is 0, The standard deviation is 1.0 The probability density function of , The standard deviation of the blue curve is larger , So it's shorter and fatter , It shows the diversity of values , And instability .
@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 Distribution
@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、 Plot all four distributions
distrs = [unif, bino, guass, bet]
for distri in distrs:
distri()Part of the code comes from official account :Python And algorithmic community
边栏推荐
- 云解决方案,为什么选择 AMD EPYC?
- Talk about study and work -- Qian Xuesen
- 倍福PLC和C#通过ADS通信传输结构体类型变量
- [daily training] 814. Binary tree pruning
- Cortex-a series processor
- 我为大厂怒刷的《100道Android面试题》
- Count different types of data according to different times (stored procedures)
- 第十天笔记
- Successful joint commissioning of Vientiane Aoke and CoDeSys Technology
- Paging collections using streams
猜你喜欢

信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)

Beifu PLC and C transmit bool array variables through ads communication

Evaluation of classification model

The context of virtual memory technology (Part 1)

射击 第 1-3 课:图像精灵

【日常训练】814. 二叉树剪枝

Numpy: quick start to basic operations

MySQL - composite query external connection

Day 10 notes

Opencv video operation
随机推荐
Beifu and C transmit real type through ads communication
The relationship between method area, perpetual generation and meta space
[jzof] 09 realize queue with two stacks
Evaluation of classification model
第十二天笔记
When using fastjson to parse and assign JSON data, the order of JSON fields is inconsistent
UI自动化
What happens when you enter the web address and the web page is displayed
Talk about study and work -- Qian Xuesen
Why does the GOM engine version automatically drop the line or flash back?
Li Kou 729. My schedule I
Is it safe to open an account with Guosen Securities software? Will the information be leaked?
【日常训练】814. 二叉树剪枝
[jzof] 08 next node of binary tree
Compilation and preprocessing
Day 12 notes
Desensitize data
Day 11 notes
Beifu PLC -- C ads communication reads variables in the form of notification
使用fastjson解析以及赋予json数据时,json字段顺序不一致问题