当前位置:网站首页>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()
结果如图所示
边栏推荐
- Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
- Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station
- link 和@improt的区别
- This is indeed the best article on microservice architecture I have read!
- Native JS takes you to understand the implementation and use of array methods
- BIO, NIO, AIO practical study notes (easy to understand theory)
- Vim tutorial: vimtutor
- 跨域的十种解决方案详解(总结)
- Q 2020, the latest senior interview Laya soul, do you know?
- Alibaba Cloud Video on Demand
猜你喜欢
input详解之文件上传
5分钟完成mysql离线安装
Successful indie developers deal with failure & imposters
Take you in-depth understanding of cookies
The hook of the operation of the selenium module
ev加密视频转换成MP4格式,亲测可用
[问题已处理]-虚拟机报错contains a file system with errors check forced
Mina disconnects and reconnects
Mina's long and short connections
One-arm routing experiment and three-layer switch experiment
随机推荐
System basics - study notes (some command records)
Tencent Cloud Message Queue CMQ
input详解之文件上传
LeetCode中常用语言的一些基本方法记录
[ingress]-ingress使用tcp端口暴露服务
Will intelligent operation and maintenance replace manual operation and maintenance?
路由器和静态路由的配置
单片机期末复习大题
What?CDN cache acceleration only works for accelerating static content?
DevOps process demo (practical record)
H5 的浏览器存储
大小屏适配
NAT experiment
7 steps to complete cloud monitoring
Autoware--Beike Tianhui rfans lidar uses the camera & lidar joint calibration file to verify the fusion effect of point cloud images
What are some things that you only know when you do operation and maintenance?
D39_ coordinate transformation
Mina disconnects and reconnects
NB-IOT智能云家具项目系列实站
One-arm routing experiment and three-layer switch experiment