当前位置:网站首页>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()
结果如图所示
边栏推荐
- LeetCode practice and self-comprehension record (1)
- DisabledDate date picker datePicker
- 大小屏适配
- [issue resolved] - jenkins pipeline checkout timeout
- What should I do if the SSL certificate prompts that it is expired or invalid?
- D39_Vector
- input detailed file upload
- 媒体查询、rem移动端适配
- Will intelligent operation and maintenance replace manual operation and maintenance?
- sql server duplicate values are counted after
猜你喜欢

Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
![[Problem has been resolved]-Virtual machine error contains a file system with errors check forced](/img/07/1222a44dd52b359bf7873e6f3b7ebf.png)
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced

What's the point of monitoring the involution of the system?

Into the pre-service, thought they play so flowers

Configuration of routers and static routes

教您简单几步实现工业树莓派正确安装RS232转USB驱动

NAT experiment

从“双卡双待“到”双通“,vivo率先推动DSDA架构落地

LeetCode刷题记录(2)

DevOps process demo (practical record)
随机推荐
time complexity and space complexity
7 steps to complete cloud monitoring
[问题已处理]-虚拟机报错contains a file system with errors check forced
Mina disconnects and reconnects
selenium模块的操作之拉钩
[ingress]-ingress exposes services using tcp port
LeetCode练习及自己理解记录(1)
错误类型:反射。ReflectionException:无法设置属性“xxx”的“类”xxx”与价值“xxx”
Q 2020, the latest senior interview Laya soul, do you know?
用户和用户组管理、文件权限管理
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
ES2020新特性
H5开发调试-Fiddler手机抓包
Next-Generation Parsing Technology - Cloud Parsing
Problems encountered in installing Yolo3 target detection module in Autoware
js 使用雪花id生成随机id
product learning materials
多行文本省略
link 和@improt的区别
Advantages of overseas servers