当前位置:网站首页>numpy.random使用文档
numpy.random使用文档
2022-08-05 05:25:00 【ProfSnail】
正态分布
二维正态分布
random.RandomState.multivariate_normal(mean, cov, size=None, check_valid='warn', tol=1e-8)
mean = (1, 2)
cov = [[1, 0], [0, 1]]
x = np.random.multivariate_normal(mean, cov, (3, 3))
第一个参数为均值向量,第二个参数为协方差矩阵。协方差矩阵参数与相关系数的关系如下。
ρ x y = C o v ( X , Y ) σ X σ Y \rho_{xy} = \dfrac{Cov(X,Y)}{\sigma_X \sigma_Y} ρxy=σXσYCov(X,Y)
下面展示不同的相关系数对生成的分布造成怎样的影响,
import numpy as np
from numpy.random import multivariate_normal
import matplotlib.pyplot as plt
def generate_data(mu1, mu2, stv1, stv2, rho, num):
cov = np.array([[stv1**2, rho*stv1*stv2],[rho*stv1*stv2, stv2**2]])
mu = np.array([mu1, mu2])
X = multivariate_normal(mu, cov, size=num)
plt.scatter(X[:,0], X[:,1], alpha=0.5, label=r'$\rho$={:.2f}'.format(rho))
for rho in [0, 0.23, 0.45, 0.68, 0.90]:
generate_data(0,0,1,1,rho,200)
plt.legend()
plt.show()
生成结果如下图:相关系数越接近1,变量之间的线性关系越明显,也就是椭球更扁平。
另外,根据多元高斯的性质,如果构成多元高斯分布,任意多个变量子集都构成高斯分布:如果独立投影到X轴和Y轴,会发现二者都服从高斯分布。
import numpy as np
from numpy.random import multivariate_normal
import matplotlib.pyplot as plt
def show_1d_gauss(mu1, mu2, stv1, stv2, rho, num, ax):
cov = np.array([[stv1**2, rho*stv1*stv2],[rho*stv1*stv2, stv2**2]])
mu = np.array([mu1, mu2])
X = multivariate_normal(mu, cov, size=num)
X1 = X[:, 0]
X2 = X[:, 1]
ax.scatter(X1, X2, alpha=0.5)
ax.scatter(X1, np.zeros_like(X1), alpha=0.2)
ax.scatter(np.zeros_like(X2), X2, alpha=0.2)
ax.set_title(r'$\rho$={:.2f}'.format(rho))
fig, axes = plt.subplots(2, 3)
rholist = [0, 0.23, 0.45, 0.68, 0.90, 1]
for i, rho in enumerate(rholist):
show_1d_gauss(0,0,1,1,rho,200, axes.flatten()[i])
plt.show()
结果如图所示
边栏推荐
- Will intelligent operation and maintenance replace manual operation and maintenance?
- The hook of the operation of the selenium module
- This is indeed the best article on microservice architecture I have read!
- VLAN介绍与实验
- VRRP overview and experiment
- time complexity and space complexity
- 错误记录集锦(遇到则记下)
- D39_Vector
- product learning materials
- Into the pre-service, thought they play so flowers
猜你喜欢
Vim tutorial: vimtutor
ALC experiment
Mina disconnects and reconnects
[问题已处理]-虚拟机报错contains a file system with errors check forced
Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
One-arm routing experiment and three-layer switch experiment
ev加密视频转换成MP4格式,亲测可用
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
LeetCode练习及自己理解记录(1)
The 25 best free games on mobile in 2020
随机推荐
[问题已处理]-jenkins流水线checkout超时
H5 的浏览器存储
摆脱极域软件的限制
Insight into the general trend of the Internet, after reading this article, you will have a thorough understanding of Chinese domain names
618,你也许可以清醒亿点点
[issue resolved] - jenkins pipeline checkout timeout
User and user group management, file permission management
干货!教您使用工业树莓派结合CODESYS配置EtherCAT主站
Disk management and file systems
Cloud Computing Basics - Study Notes
媒体查询、rem移动端适配
The highlight moment of operation and maintenance starts with intelligence
NB-IOT智能云家具项目系列实站
Mina disconnects and reconnects
跨域的十种解决方案详解(总结)
产品学习资料
This is indeed the best article on microservice architecture I have read!
数组&的运算
BIO,NIO,AIO实践学习笔记(便于理解理论)
人人AI(吴恩达系列)