|
26 | 26 |
|
27 | 27 | > 后续章节: |
28 | 28 | > |
29 | | -> 1) 不断丰富原有1~7章节; |
30 | | -> 2) Python基础算法; |
31 | | -> 3) python 机器学习,包括机器学习的基础概念和十大核心算法以及Sklearn和Kaggle实战的小例子。 |
32 | | -> 4) PyQt制作GUI |
33 | | -> 5) Flask前端开发 |
34 | | -> 6) Python数据分析:NumPy, Pandas, Matplotlib, Plotly等 |
| 29 | +> 1) 不断丰富原有1~7章节 |
| 30 | +> 2) PyQt制作GUI |
| 31 | +> 3) Flask前端开发 |
| 32 | +> 4) Python数据分析 |
35 | 33 |
|
36 | 34 |
|
37 | 35 | 已发[《Python之路.1.1.pdf》](https://github.com/jackzhenguo/python-small-examples/releases/tag/V1.1)最新版本包括7个章节:`Python基础`,`Python字符串和正则`,`Python文件`,`Python日期`, `Python利器`,`Python画图` 章节,共计`147个`小例子。 |
@@ -5141,3 +5139,99 @@ for sort_method in [bubble_sort, quick_sort, selection_sort, heap_sort]: |
5141 | 5139 |
|
5142 | 5140 |
|
5143 | 5141 |
|
| 5142 | +#### 8 均匀分布 |
| 5143 | +
|
| 5144 | +导入本次实验所用的4种常见分布,连续分布的代表:`beta`分布、`正态`分布,`均匀`分布,离散分布的代表:`二项`分布。 |
| 5145 | +
|
| 5146 | +绘图装饰器带有四个参数分别表示`legend`的2类说明文字,y轴label, 保存的png文件名称。 |
| 5147 | +
|
| 5148 | +```python |
| 5149 | +import pretty_errors |
| 5150 | +import numpy as np |
| 5151 | +from scipy.stats import beta, norm, uniform, binom |
| 5152 | +import matplotlib.pyplot as plt |
| 5153 | +from functools import wraps |
| 5154 | +
|
| 5155 | +# 定义带四个参数的画图装饰器 |
| 5156 | +
|
| 5157 | +def my_plot(label0=None, label1=None, ylabel='probability density function', fn=None): |
| 5158 | + def decorate(f): |
| 5159 | + @wraps(f) |
| 5160 | + def myplot(): |
| 5161 | + fig = plt.figure(figsize=(16, 9)) |
| 5162 | + ax = fig.add_subplot(111) |
| 5163 | + x, y, y1 = f() |
| 5164 | + ax.plot(x, y, linewidth=2, c='r', label=label0) |
| 5165 | + ax.plot(x, y1, linewidth=2, c='b', label=label1) |
| 5166 | + ax.legend() |
| 5167 | + plt.ylabel(ylabel) |
| 5168 | + # plt.show() |
| 5169 | + plt.savefig('./img/%s' % (fn,)) |
| 5170 | + print('%s保存成功' % (fn,)) |
| 5171 | + plt.close() |
| 5172 | + return myplot |
| 5173 | + return decorate |
| 5174 | +``` |
| 5175 | +
|
| 5176 | +```python |
| 5177 | +# 均匀分布(uniform) |
| 5178 | +@my_plot(label0='b-a=1.0', label1='b-a=2.0', fn='uniform.png') |
| 5179 | +def unif(): |
| 5180 | + x = np.arange(-0.01, 2.01, 0.01) |
| 5181 | + y = uniform.pdf(x, loc=0.0, scale=1.0) |
| 5182 | + y1 = uniform.pdf(x, loc=0.0, scale=2.0) |
| 5183 | + return x, y, y1 |
| 5184 | +``` |
| 5185 | +
|
| 5186 | + |
| 5187 | +
|
| 5188 | +#### 9 **二项分布** |
| 5189 | +
|
| 5190 | +红色曲线表示发生一次概率为0.3,重复50次的密度函数,二项分布期望值为0.3*50 = 15次。看到这50次实验,很可能出现的次数为10~20.可与蓝色曲线对比分析。 |
| 5191 | +
|
| 5192 | +```python |
| 5193 | +# 二项分布 |
| 5194 | +@my_plot(label0='n=50,p=0.3', label1='n=50,p=0.7', fn='binom.png', ylabel='probability mass function') |
| 5195 | +def bino(): |
| 5196 | + x = np.arange(50) |
| 5197 | + n, p, p1 = 50, 0.3, 0.7 |
| 5198 | + y = binom.pmf(x, n=n, p=p) |
| 5199 | + y1 = binom.pmf(x, n=n, p=p1) |
| 5200 | + return x, y, y1 |
| 5201 | +``` |
| 5202 | +
|
| 5203 | + |
| 5204 | +
|
| 5205 | +#### 10 高斯分布 |
| 5206 | +
|
| 5207 | +红色曲线表示均值为0,标准差为1.0的概率密度函数,蓝色曲线的标准差更大,所以它更矮胖,显示出取值的多样性,和不稳定性。 |
| 5208 | +
|
| 5209 | +```python |
| 5210 | +# 高斯 分布 |
| 5211 | +@my_plot(label0='u=0.,sigma=1.0', label1='u=0.,sigma=2.0', fn='guass.png') |
| 5212 | +def guass(): |
| 5213 | + x = np.arange(-5, 5, 0.1) |
| 5214 | + y = norm.pdf(x, loc=0.0, scale=1.0) |
| 5215 | + y1 = norm.pdf(x, loc=0., scale=2.0) |
| 5216 | + return x, y, y1 |
| 5217 | +``` |
| 5218 | +
|
| 5219 | + |
| 5220 | +
|
| 5221 | +#### 11 beta分布 |
| 5222 | +
|
| 5223 | +beta分布的期望值如下,可从下面的两条曲线中加以验证: |
| 5224 | +
|
| 5225 | + |
| 5226 | +
|
| 5227 | +```python |
| 5228 | +# beta 分布 |
| 5229 | +@my_plot(label0='a=10., b=30.', label1='a=4., b=4.', fn='beta.png') |
| 5230 | +def bet(): |
| 5231 | + x = np.arange(-0.01, 1, 0.001) |
| 5232 | + y = beta.pdf(x, a=10., b=30.) |
| 5233 | + y1 = beta.pdf(x, a=4., b=4.) |
| 5234 | + return x, y, y1 |
| 5235 | +``` |
| 5236 | +
|
| 5237 | + |
0 commit comments