当前位置:网站首页>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()
结果如图所示
边栏推荐
猜你喜欢
随机推荐
网络协议基础-学习笔记
One-arm routing experiment and three-layer switch experiment
云计算基础-学习笔记
[issue resolved] - jenkins pipeline checkout timeout
VLAN is introduced with the experiment
product learning materials
BIO, NIO, AIO practical study notes (easy to understand theory)
Alibaba Cloud Video on Demand
网络排错基础-学习笔记
Q 2020, the latest senior interview Laya soul, do you know?
LeetCode practice and self-comprehension record (1)
[ingress]-ingress使用tcp端口暴露服务
Collection of error records (write down when you encounter them)
May I ask how to read the binlog of the two tables of hologres through flink sql, and then how to join?
selenium模块的操作之拉钩
前置++和后置++的区别
Mina断线重连
Passing parameters in multiple threads
From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
无法导入torchvision.io.read_image