python坐标系 向量分量_计算多元正态分布的均值向量python

“Phi”我相信你的意思是你想估计的概率密度函数(pdf)。在这种情况下,协方差矩阵应为M×M并且输出披将NX1:

# -*- coding: utf-8 -*-

import numpy as np

N = 1024

M = 8

var = 0.5

# Creating a Xtrain NxM observation matrix.

# Its muVector is [0, 1, 2, 3, 4, 5, 6, 7] and the variance for all

# independent random variables is 0.5.

Xtrain = np.random.multivariate_normal(np.arange(8), np.eye(8,8)*var, N)

# Estimating the mean vector.

muVector = np.mean(Xtrain, axis=0)

# Creating the estimated covariance matrix and its inverse.

cov = np.eye(M,M)*var

inv_cov = np.linalg.inv(cov)

# Normalization factor from the pdf.

norm_factor = 1/np.sqrt((2*np.pi)**M * np.linalg.det(cov))

# Estimating the pdf.

Phi = np.ones((N,1))

for row in range(N):

temp = Xtrain[row,:] - muVector

temp.shape = (1,M)

temp = np.dot(-0.5*temp, inv_cov)

temp = np.dot(temp, (Xtrain[row,:] - muVector))

Phi[row] = norm_factor*np.exp(temp)

或者,也可以使用pdf方法从scipy.stats.multivariate_normal:

# -*- coding: utf-8 -*-

import numpy as np

from scipy.stats import multivariate_normal

N = 1024

M = 8

var = 0.5

# Creating a Xtrain NxM observation matrix.

# Its muVector is [0, 1, 2, 3, 4, 5, 6, 7] and the variance for all

# independent random variables is 0.5.

Xtrain = np.random.multivariate_normal(np.arange(8), np.eye(8,8)*var, N)

# Estimating the mean vector.

muVector = np.mean(Xtrain, axis=0)

# Creating the estimated covariance matrix.

cov = np.eye(M,M)*var

Phi2 = multivariate_normal.pdf(Xtrain, mean=muVector, cov=cov)

两个Phi和Phi2输出阵列将等于。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值