当前位置:网站首页>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()
结果如图所示
边栏推荐
- The cocos interview answers you are looking for are all here!
- DisabledDate date picker datePicker
- 设置文本向两边居中展示
- 浏览器存储WebStorage
- Quick question and quick answer - FAQ of Tencent Cloud Server
- 记录vue-页面缓存问题
- Media query, rem mobile terminal adaptation
- 网络层协议介绍
- From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
- Successful indie developers deal with failure & imposters
猜你喜欢
LeetCode practice and self-comprehension record (1)
selenium learning
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
The use of three parameters of ref, out, and Params in Unity3D
vs2017关于函数命名方面的注意事项
DevOps-了解学习
DevOps流程demo(实操记录)
5分钟完成mysql离线安装
NB-IOT智能云家具项目系列实站
Cocos Creator Mini Game Case "Stick Soldier"
随机推荐
LeetCode练习及自己理解记录(1)
带你深入了解Cookie
Disk management and file systems
Problems encountered in installing Yolo3 target detection module in Autoware
H5开发调试-Fiddler手机抓包
Wireshark packet capture and common filtering methods
Next-Generation Parsing Technology - Cloud Parsing
系统基础-学习笔记(一些命令记录)
By solving these three problems, the operation and maintenance efficiency will exceed 90% of the hospital
LeetCode practice and self-comprehension record (1)
D46_Force applied to rigid body
What impact does CIPU have on the cloud computing industry?
Cloud computing - osi seven layers and TCP\IP protocol
七种让盒子水平垂直居中的方法
sql server duplicate values are counted after
Complete mysql offline installation in 5 minutes
The method of using ROS1 bag under ROS2
Four ways to obtain Class objects through reflection
指针常量与常量指针 巧记
D39_Eulerian Angles and Quaternions