gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:54:45 25 4
gpt4 key购买 nike

我无法将多元高斯分布拟合到我的数据集,更具体地说,是找到一个均值向量(或多个均值向量)。我的数据集是一个 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()?但我不知道在这种情况下如何使用它。

最佳答案

我认为“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 输出数组都将相等。

关于python - 计算多元正态分布python的均值向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28402167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com