使用python模擬高斯分布例子
正態(tài)分布(Normal distribution),也稱“常態(tài)分布”,又名高斯分布(Gaussian distribution)
正態(tài)曲線呈鐘型,兩頭低,中間高,左右對(duì)稱因其曲線呈鐘形,因此人們又經(jīng)常稱之為鐘形曲線。
若隨機(jī)變量X服從一個(gè)數(shù)學(xué)期望為μ、方差為σ^2的正態(tài)分布。其概率密度函數(shù)為正態(tài)分布的期望值μ決定了其位置,其標(biāo)準(zhǔn)差σ決定了分布的幅度。當(dāng)μ = 0,σ = 1時(shí)的正態(tài)分布是標(biāo)準(zhǔn)正態(tài)分布。
用python 模擬
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
from scipy import stats
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import seaborn
def calc_statistics(x):
n = x.shape[0] # 樣本個(gè)數(shù)
# 手動(dòng)計(jì)算
m = 0
m2 = 0
m3 = 0
m4 = 0
for t in x:
m += t
m2 += t*t
m3 += t**3
m4 += t**4
m /= n
m2 /= n
m3 /= n
m4 /= n
mu = m
sigma = np.sqrt(m2 - mu*mu)
skew = (m3 - 3*mu*m2 + 2*mu**3) / sigma**3
kurtosis = (m4 - 4*mu*m3 + 6*mu*mu*m2 - 4*mu**3*mu + mu**4) / sigma**4 - 3
print('手動(dòng)計(jì)算均值、標(biāo)準(zhǔn)差、偏度、峰度:', mu, sigma, skew, kurtosis)
# 使用系統(tǒng)函數(shù)驗(yàn)證
mu = np.mean(x, axis=0)
sigma = np.std(x, axis=0)
skew = stats.skew(x)
kurtosis = stats.kurtosis(x)
return mu, sigma, skew, kurtosis
if __name__ == '__main__':
d = np.random.randn(10000)
print(d)
print(d.shape)
mu, sigma, skew, kurtosis = calc_statistics(d)
print('函數(shù)庫(kù)計(jì)算均值、標(biāo)準(zhǔn)差、偏度、峰度:', mu, sigma, skew, kurtosis)
# 一維直方圖
mpl.rcParams['font.sans-serif'] = 'SimHei'
mpl.rcParams['axes.unicode_minus'] = False
plt.figure(num=1, facecolor='w')
y1, x1, dummy = plt.hist(d, bins=30, normed=True, color='g', alpha=0.75, edgecolor='k', lw=0.5)
t = np.arange(x1.min(), x1.max(), 0.05)
y = np.exp(-t**2 / 2) / math.sqrt(2*math.pi)
plt.plot(t, y, 'r-', lw=2)
plt.title('高斯分布,樣本個(gè)數(shù):%d' % d.shape[0])
plt.grid(b=True, ls=':', color='#404040')
# plt.show()
d = np.random.randn(100000, 2)
mu, sigma, skew, kurtosis = calc_statistics(d)
print('函數(shù)庫(kù)計(jì)算均值、標(biāo)準(zhǔn)差、偏度、峰度:', mu, sigma, skew, kurtosis)
# 二維圖像
N = 30
density, edges = np.histogramdd(d, bins=[N, N])
print('樣本總數(shù):', np.sum(density))
density /= density.max()
x = y = np.arange(N)
print('x = ', x)
print('y = ', y)
t = np.meshgrid(x, y)
print(t)
fig = plt.figure(facecolor='w')
ax = fig.add_subplot(111, projection='3d')
# ax.scatter(t[0], t[1], density, c='r', s=50*density, marker='o', depthshade=True, edgecolor='k')
ax.plot_surface(t[0], t[1], density, cmap=cm.Accent, rstride=1, cstride=1, alpha=0.9, lw=0.75, edgecolor='k')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title('二元高斯分布,樣本個(gè)數(shù):%d' % d.shape[0], fontsize=15)
plt.tight_layout(0.1)
plt.show()


來(lái)個(gè)6的
二元高斯分布方差比較
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
if __name__ == '__main__':
x1, x2 = np.mgrid[-5:5:51j, -5:5:51j]
x = np.stack((x1, x2), axis=2)
print('x1 = \n', x1)
print('x2 = \n', x2)
print('x = \n', x)
mpl.rcParams['axes.unicode_minus'] = False
mpl.rcParams['font.sans-serif'] = 'SimHei'
plt.figure(figsize=(9, 8), facecolor='w')
sigma = (np.identity(2), np.diag((3,3)), np.diag((2,5)), np.array(((2,1), (1,5))))
for i in np.arange(4):
ax = plt.subplot(2, 2, i+1, projection='3d')
norm = stats.multivariate_normal((0, 0), sigma[i])
y = norm.pdf(x)
ax.plot_surface(x1, x2, y, cmap=cm.Accent, rstride=1, cstride=1, alpha=0.9, lw=0.3, edgecolor='#303030')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.suptitle('二元高斯分布方差比較', fontsize=18)
plt.tight_layout(1.5)
plt.show()

圖像好看嗎?
以上這篇使用python模擬高斯分布例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何用Python和JS實(shí)現(xiàn)的Web SSH工具
這篇文章主要介紹了如何用Python和JS實(shí)現(xiàn)的Web SSH工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
torchxrayvision包安裝過(guò)程(附pytorch1.6cpu版安裝)
這篇文章主要介紹了torchxrayvision包安裝過(guò)程(附pytorch1.6cpu版安裝),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
使用 Python 讀取電子表格中的數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了使用 Python 讀取電子表格中的數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
python shapely.geometry.polygon任意兩個(gè)四邊形的IOU計(jì)算實(shí)例
這篇文章主要介紹了python shapely.geometry.polygon任意兩個(gè)四邊形的IOU計(jì)算實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python實(shí)用小知識(shí)之對(duì)象間的比較
== 和 is 是 Python 中對(duì)象比較常用的兩種方式,簡(jiǎn)單來(lái)說(shuō),'==' 操作符比較對(duì)象之間的值是否相等,下面這篇文章主要給大家介紹了關(guān)于Python實(shí)用小知識(shí)之對(duì)象間比較的相關(guān)資料,需要的朋友可以參考下2021-10-10
python實(shí)現(xiàn)生成Word、docx文件的方法分析
這篇文章主要介紹了python實(shí)現(xiàn)生成Word、docx文件的方法,結(jié)合實(shí)例形式分析了Python使用docx模塊操作word文件與docx文件的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
Python運(yùn)維自動(dòng)化之paramiko模塊應(yīng)用實(shí)例
paramiko是一個(gè)基于SSH用于連接遠(yuǎn)程服務(wù)器并執(zhí)行相關(guān)操作,使用該模塊可以對(duì)遠(yuǎn)程服務(wù)器進(jìn)行命令或文件操作,這篇文章主要給大家介紹了關(guān)于Python運(yùn)維自動(dòng)化之paramiko模塊應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-09-09
python獲取redis memory使用情況場(chǎng)景分析
這篇文章主要介紹了python獲取redis memory使用情況,項(xiàng)目研發(fā)過(guò)程中,用到Python操作Redis場(chǎng)景,記錄學(xué)習(xí)過(guò)程中的心得體會(huì),需要的朋友可以參考下2022-12-12
Python中內(nèi)置函數(shù)filter函數(shù)用法詳解
filter()函數(shù)是Python內(nèi)置的另一個(gè)有用的高階函數(shù),filter()函數(shù)接收一個(gè)函數(shù)f和一個(gè)序列,函數(shù)f的作用是對(duì)每個(gè)元素進(jìn)行判斷,返回True或False,下面這篇文章主要給大家介紹了關(guān)于Python中內(nèi)置函數(shù)filter函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2024-05-05

