当前位置:网站首页>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()
结果如图所示
边栏推荐
- LeetCode practice and self-comprehension record (1)
- vs2017关于函数命名方面的注意事项
- H5 的浏览器存储
- cs231n学习记录
- input detailed file upload
- vscode笔记
- link 和@improt的区别
- selenium learning
- From "dual card dual standby" to "dual communication", vivo took the lead in promoting the implementation of the DSDA architecture
- transport layer protocol
猜你喜欢
随机推荐
Come, come, let you understand how Cocos Creator reads and writes JSON files
媒体查询、rem移动端适配
ROS2下使用ROS1 bag的方法
Some basic method records of commonly used languages in LeetCode
The use of three parameters of ref, out, and Params in Unity3D
Browser Storage WebStorage
Error correction notes for the book Image Processing, Analysis and Machine Vision
D39_ coordinate transformation
Problems encountered in installing Yolo3 target detection module in Autoware
NB-IOT智能云家具项目系列实站
文件内音频的时长统计并生成csv文件
DevOps - Understanding Learning
Nacos配置服务的源码解析(全)
The future of cloud gaming
UI刘海屏适配方式
BIO,NIO,AIO实践学习笔记(便于理解理论)
Configuration of routers and static routes
Shadowless Cloud Desktop
ALC experiment
指针常量与常量指针 巧记








![[issue resolved] - jenkins pipeline checkout timeout](/img/3d/c14276d2b5ce18fc3d1288abb059c0.png)