计算多元正态分布的均值向量Python

3
我遇到了一些困难,无法将多元高斯分布适配到我的数据集上,更具体地说,是找到一个均值向量(或多个均值向量)。我的数据集是一个N x 8的矩阵,目前我正在使用以下代码:muVector = np.mean(Xtrain, axis=0) 其中Xtrain是我的训练数据集。
对于协方差,我使用任意的方差值(.5)进行构建,然后执行以下操作:covariance = np.dot(.5, np.eye(N,N)) 其中N是观察数量。
但是当我构建Phi矩阵时,我得到的全部都是零。下面是我的代码:
muVector = np.mean(Xtrain, axis=0)
# get covariance matrix from Xtrain
cov = np.dot(var, np.eye(N,N))
cov = np.linalg.inv(cov)  

# build Xtrain Phi
Phi = np.ones((N,M))
for row in range(N):
  temp = Xtrain[row,:] - muVector
  temp.shape = (1,M)
  temp = np.dot((-.5), temp)
  temp = np.dot(temp, cov)
  temp = np.dot(temp, (Xtrain[row,:] - muVector))
  Phi[row,:] = np.exp(temp)

非常感谢您的帮助。我认为我可能需要使用np.random.multivariate_normal()函数?但是我不知道在这种情况下如何使用它。

1个回答

3

我认为你所说的“Phi”是指要估计的概率密度函数(pdf)。在这种情况下,协方差矩阵应该是MxM,输出的Phi将是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)

或者,您可以使用scipy.stats.multivariate_normal中的pdf方法:

# -*- 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)

PhiPhi2输出的数组将相等。


非常感谢!这正是我在寻找的东西。 - frylock405

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接