当前位置:网站首页>numpy.random usage documentation
numpy.random usage documentation
2022-08-05 06:45: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))
The first parameter is the mean vector,The second parameter is the covariance matrix.The relationship between the covariance matrix parameters and the correlation coefficient is as follows.
ρ x y = C o v ( X , Y ) σ X σ Y \rho_{xy} = \dfrac{Cov(X,Y)}{\sigma_X \sigma_Y} ρxy=σXσYCov(X,Y)
The following shows how different correlation coefficients affect the resulting distribution,
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,The more obvious the linear relationship between the variables,That is, the ellipsoid is flatter.
另外,According to the properties of the multivariate Gaussian,If a multivariate Gaussian distribution is formed,Any subset of variables constitutes a Gaussian distribution:If independently projected toX轴和Y轴,It will be found that both obey a Gaussian distribution.
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()
结果如图所示
边栏推荐
- Billions of IT operations in the market, the product by strength to speak
- Introduction to Network Layer Protocols
- reduce()方法的学习和整理
- 通过反射获取Class对象的四种方式
- Problems encountered in installing Yolo3 target detection module in Autoware
- 无法导入torchvision.io.read_image
- disabledDate 日期选择器 datePicker
- Writing OpenCV in VSCode
- [问题已处理]-jenkins流水线checkout超时
- Network Protocol Fundamentals - Study Notes
猜你喜欢
随机推荐
System basics - study notes (some command records)
DevOps流程demo(实操记录)
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
浏览器存储WebStorage
Problems encountered in installing Yolo3 target detection module in Autoware
Matplotlib绘图笔记
Detailed explanation of ten solutions across domains (summary)
[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
link 和@improt的区别
LaTeX笔记
Billions of IT operations in the market, the product by strength to speak
The size of the screen adaptation
el-progress implements different colors of the progress bar
ALC experiment
【FAQ】什么是 Canon CCAPI
网络协议基础-学习笔记
D39_Eulerian Angles and Quaternions
transport layer protocol
摆脱极域软件的限制









