2015-02-09 126 views
2

我無法將多元高斯分佈擬合到我的數據集中,更具體地說,找到一個平均向量(或多個平均向量)。我的數據集是N×8矩陣,目前我正在使用此代碼:計算多元正態分佈的均值向量python

muVector = np.mean(Xtrain, axis=0)其中Xtrain是我的訓練數據集。

對於我使用的任意的分散值(0.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()?但在這種情況下我不知道如何使用它。

回答

0

「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) 

兩個PhiPhi2輸出陣列將等於。

+0

非常感謝!這正是我所期待的。 – frylock405 2015-02-13 04:22:18

+0

好吧,這很好,它的工作=)! – bjbschmitt 2015-02-13 10:24:30