当前位置:网站首页>numpy.random使用文档
numpy.random使用文档
2022-08-05 05:25: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))
第一个参数为均值向量,第二个参数为协方差矩阵。协方差矩阵参数与相关系数的关系如下。
ρ x y = C o v ( X , Y ) σ X σ Y \rho_{xy} = \dfrac{Cov(X,Y)}{\sigma_X \sigma_Y} ρxy=σXσYCov(X,Y)
下面展示不同的相关系数对生成的分布造成怎样的影响,
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,变量之间的线性关系越明显,也就是椭球更扁平。
另外,根据多元高斯的性质,如果构成多元高斯分布,任意多个变量子集都构成高斯分布:如果独立投影到X轴和Y轴,会发现二者都服从高斯分布。
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()
结果如图所示
边栏推荐
- 设置文本向两边居中展示
- Transformer interprets and predicts instance records in detail
- Difference between link and @improt
- King power volume LinkSLA, realize operations engineer is happy fishing
- Take you in-depth understanding of cookies
- Configuration of routers and static routes
- el-progress implements different colors of the progress bar
- Network Protocol Fundamentals - Study Notes
- ROS2下使用ROS1 bag的方法
- 教您简单几步实现工业树莓派正确安装RS232转USB驱动
猜你喜欢

Growth: IT Operations Trends Report
![[issue resolved] - jenkins pipeline checkout timeout](/img/3d/c14276d2b5ce18fc3d1288abb059c0.png)
[issue resolved] - jenkins pipeline checkout timeout

VLAN介绍与实验

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

transport layer protocol
![[问题已处理]-虚拟机报错contains a file system with errors check forced](/img/07/1222a44dd52b359bf7873e6f3b7ebf.png)
[问题已处理]-虚拟机报错contains a file system with errors check forced

sql server duplicate values are counted after

Mina断线重连

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

Mina's long and short connections
随机推荐
In-depth Zabbix user guide - from the green boy
错误记录集锦(遇到则记下)
Will intelligent operation and maintenance replace manual operation and maintenance?
网络排错基础-学习笔记
The use of three parameters of ref, out, and Params in Unity3D
Cocos Creator Mini Game Case "Stick Soldier"
What are some things that you only know when you do operation and maintenance?
Difference between link and @improt
Cloud Computing Basics - Study Notes
RAID disk array
Mina断线重连
VRRP overview and experiment
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
人人AI(吴恩达系列)
Take you in-depth understanding of cookies
5分钟完成mysql离线安装
VLAN is introduced with the experiment
Cloud computing - osi seven layers and TCP\IP protocol
数组&的运算
【考研结束第一天,过于空虚,想对自己进行总结一下】