当前位置:网站首页>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()
结果如图所示
边栏推荐
- ## 简讲protobuf-从原理到使用
- 错误记录集锦(遇到则记下)
- link 和@improt的区别
- 产品学习资料
- DisabledDate date picker datePicker
- Error correction notes for the book Image Processing, Analysis and Machine Vision
- Four ways to obtain Class objects through reflection
- [Problem has been resolved]-Virtual machine error contains a file system with errors check forced
- el-autocomplete使用
- Some basic method records of commonly used languages in LeetCode
猜你喜欢
随机推荐
DevOps流程demo(实操记录)
selenium学习
Successful indie developers deal with failure & imposters
link 和@improt的区别
滚动条问题,未解决
跨域的十种解决方案详解(总结)
The hook of the operation of the selenium module
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
深夜小酌,50道经典SQL题,真香~
disabledDate 日期选择器 datePicker
flink cdc 目前支持Gauss数据库源吗
图像处理、分析与机器视觉一书纠错笔记
sql server duplicate values are counted after
Matplotlib绘图笔记
Nacos集群搭建
docker部署完mysql无法连接
Mina断线重连
错误记录集锦(遇到则记下)
ALC experiment
单片机期末复习大题