Ben*_*nno 49 python numpy scipy
是否有任何python包允许有效计算多变量正常pdf?
它似乎没有被包含在Numpy/Scipy中,并且令人惊讶的是谷歌搜索没有发现任何有用的东西.
jul*_*ohm 67
多变量法线现在可用于SciPy 0.14.0.dev-16fc0af
:
from scipy.stats import multivariate_normal
var = multivariate_normal(mean=[0,0], cov=[[1,0],[0,1]])
var.pdf([1,0])
Run Code Online (Sandbox Code Playgroud)
use*_*734 20
我只为我的目的做了一个,所以我虽然分享.它是使用numpy的"权力"构建的,来自http://en.wikipedia.org/wiki/Multivariate_normal_distribution的非退化案例的公式,它aso验证输入.
这是代码和示例运行
from numpy import *
import math
# covariance matrix
sigma = matrix([[2.3, 0, 0, 0],
[0, 1.5, 0, 0],
[0, 0, 1.7, 0],
[0, 0, 0, 2]
])
# mean vector
mu = array([2,3,8,10])
# input
x = array([2.1,3.5,8, 9.5])
def norm_pdf_multivariate(x, mu, sigma):
size = len(x)
if size == len(mu) and (size, size) == sigma.shape:
det = linalg.det(sigma)
if det == 0:
raise NameError("The covariance matrix can't be singular")
norm_const = 1.0/ ( math.pow((2*pi),float(size)/2) * math.pow(det,1.0/2) )
x_mu = matrix(x - mu)
inv = sigma.I
result = math.pow(math.e, -0.5 * (x_mu * inv * x_mu.T))
return norm_const * result
else:
raise NameError("The dimensions of the input don't match")
print norm_pdf_multivariate(x, mu, sigma)
Run Code Online (Sandbox Code Playgroud)
在对角协方差矩阵的常见情况下,可以通过简单地乘以scipy.stats.norm
实例返回的单变量PDF值来获得多变量PDF .如果您需要一般情况,您可能需要自己编写代码(这应该不难).
小智 7
如果仍然需要,我的实施将是
import numpy as np
def pdf_multivariate_gauss(x, mu, cov):
'''
Caculate the multivariate normal density (pdf)
Keyword arguments:
x = numpy array of a "d x 1" sample vector
mu = numpy array of a "d x 1" mean vector
cov = "numpy array of a d x d" covariance matrix
'''
assert(mu.shape[0] > mu.shape[1]), 'mu must be a row vector'
assert(x.shape[0] > x.shape[1]), 'x must be a row vector'
assert(cov.shape[0] == cov.shape[1]), 'covariance matrix must be square'
assert(mu.shape[0] == cov.shape[0]), 'cov_mat and mu_vec must have the same dimensions'
assert(mu.shape[0] == x.shape[0]), 'mu and x must have the same dimensions'
part1 = 1 / ( ((2* np.pi)**(len(mu)/2)) * (np.linalg.det(cov)**(1/2)) )
part2 = (-1/2) * ((x-mu).T.dot(np.linalg.inv(cov))).dot((x-mu))
return float(part1 * np.exp(part2))
def test_gauss_pdf():
x = np.array([[0],[0]])
mu = np.array([[0],[0]])
cov = np.eye(2)
print(pdf_multivariate_gauss(x, mu, cov))
# prints 0.15915494309189535
if __name__ == '__main__':
test_gauss_pdf()
Run Code Online (Sandbox Code Playgroud)
如果我将来进行更改,代码将在GitHub上
小智 7
您可以使用 numpy 轻松计算。我为了机器学习课程的目的实现了如下,并想分享,希望对某人有所帮助。
import numpy as np
X = np.array([[13.04681517, 14.74115241],[13.40852019, 13.7632696 ],[14.19591481, 15.85318113],[14.91470077, 16.17425987]])
def est_gaus_par(X):
mu = np.mean(X,axis=0)
sig = np.std(X,axis=0)
return mu,sig
mu,sigma = est_gaus_par(X)
def est_mult_gaus(X,mu,sigma):
m = len(mu)
sigma2 = np.diag(sigma)
X = X-mu.T
p = 1/((2*np.pi)**(m/2)*np.linalg.det(sigma2)**(0.5))*np.exp(-0.5*np.sum(X.dot(np.linalg.pinv(sigma2))*X,axis=1))
return p
p = est_mult_gaus(X, mu, sigma)
Run Code Online (Sandbox Code Playgroud)