当前位置:网站首页>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()
结果如图所示
边栏推荐
猜你喜欢

ALC experiment

NB-IOT智能云家具项目系列实站

numpy.random使用文档

LeetCode练习及自己理解记录(1)

自营商城提高用户留存小技巧,商城对接小游戏分享

花花省V5淘宝客APP源码无加密社交电商自营商城系统带抖音接口

From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture

transport layer protocol

selenium学习

disabledDate 日期选择器 datePicker
随机推荐
reduce()方法的学习和整理
VSCode编写OpenCV
图像处理、分析与机器视觉一书纠错笔记
媒体查询、rem移动端适配
sql server duplicate values are counted after
product learning materials
Configuration of routers and static routes
Into the pre-service, thought they play so flowers
设置文本向两边居中展示
[问题已处理]-jenkins流水线checkout超时
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
教您简单几步实现工业树莓派正确安装RS232转USB驱动
scikit-image图像处理笔记
无法导入torchvision.io.read_image
Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
UI刘海屏适配方式
H5开发调试-Fiddler手机抓包
Browser Storage WebStorage
ES2020新特性
input detailed file upload