实例_高斯分布(正态分布)_Python
2018-07-18 本文已影响1299人
违规昵称不予展示
# 用命令行写的
#使用for循环随机产生100条高斯分布原始数据
>>> for i in range(100):
... data.append(np.round(random.gauss(0,800),2))
...
>>> data
[279.92, -574.04, -667.95, -936.72, -1618.42, -230.06, 908.0, -338.68, 596.47, 3.67, -195.46, -437.21, -1489.73, 601.55, -856.9, 80.7, -118.76, -741.37, 361.06, 816.93, -501.94, 722.49, 677.43, -431.13, 888.12, 242.01, -186.59, 117.41, -113.76, 162.59, 114.94, 273.89, -1091.75, 971.92, 375.75, 590.55, -798.58, -145.41, 383.01, 2.98, 309.08, -226.7, 944.2, 282.6, 452.85, 275.87, 149.92, 504.1, 643.84, 942.69, -170.35, -917.85, -1208.69, -754.38, -432.77, -1674.96, -592.45, -217.46, 539.93, 113.98, -176.5, -862.39, -765.25, 1389.26, -361.02, -435.82, 1200.76, 179.56, -530.13, -774.71, -125.61, -144.03, -1241.34, 486.65, 45.54, 407.84, -1572.21, -1262.15, -631.28, 182.49, -281.63, 590.96, 1098.1, -528.07, -201.9, -309.04, 991.28, -950.37, 797.57, 313.76, 183.15, -234.85, -216.02, -879.63, -1224.94, -189.44, 332.13, -276.18, -256.63, -934.17]
>>> max(data) # 求出最大值
1389.26
>>> min(data) # 求出最小值
-1674.96
>>> print 1389.26+1674.96 # emmmmm
3064.22
>>> print 1400+1700 # 计算大概的最左最右间距吧,不是太严格
3100
>>> print 3100/10 # 分成10组
310 # 每组的间隔
>>> 0-310*5 # emmmmm
-1550
>>> a = -1700 # 第一组的起点
>>> k = 310 # 每组的宽度,当然也是第一组的宽度
>>> y = [] # 定义一个list存放每一组的统计量
# 统计每一组的高度,就是每一个区间所包含原始数据的个数啦
>>> for k in range(10):
... y.append(len([i for i in data if (a+k*310)<i<(a+310+k*310)]))
...
>>> print y # Y轴数据
[4, 5, 9, 10, 21, 13, 17, 10, 8, 3]
>>> x # emmm
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = [] # 定义x轴
>>> for k in range(10):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
# 下面是计算x的过程,x就是每一个分组的中点
>>> b = -1700+160
>>> b
-1540
>>> for k in range(10):
... x.append(b)
... b += 310
...
>>> x
[-1540, -1230, -920, -610, -300, 10, 320, 630, 940, 1250]
>>> a # emmmm
-1700
>>> a+310+9*310
1400
>>> x
[-1540, -1230, -920, -610, -300, 10, 320, 630, 940, 1250]
>>> y
[4, 5, 9, 10, 21, 13, 17, 10, 8, 3]
>>> plt.plot(x,y,'g--H') # 开始画图 需要事先导入包 mport matplotlib.pyplot as plt
[<matplotlib.lines.Line2D object at 0x0000000016B40588>]
>>> plt.show()
image.png
最后附上一个高斯函数的实现
def gaussian(sigma, x, u):
y = np.exp(-(x - u) ** 2 / (2 * sigma ** 2)) / (sigma * math.sqrt(2 * math.pi))
return y