我在用scipy定义求积的多元高斯pdf时遇到了一些困难。我写了一个函数,它以平均向量和协方差矩阵作为输入,并返回一个高斯函数。在def make_mvn_pdf(mu, sigma):
def f(x):
return sp.stats.multivariate_normal.pdf(x, mu, sigma)
return f
当我使用make_mvn_pdf来定义高斯函数并尝试索引到高斯函数时,我得到了一个没有意义的错误。我首先定义一个平均向量和协方差矩阵,并将它们传递到make \u mvn_pdf中:
^{pr2}$
我试着把观测值传递到密度上,以得到概率:testarray=np.random.random((2,2))
g_int_func(testarray)
这将返回以下我不理解的错误。在---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
1 g_int_func = make_mvn_pdf(np.dot(B,Obs[t,:]),Gamma)
----> 2 g_int_func(testarray)
/Users/...in f(x)
17 def make_mvn_pdf(mu, sigma):
18 def f(x):
---> 19 return sp.stats.multivariate_normal.pdf(x, mu, sigma)
20 return f
21
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in pdf(self, x, mean, cov, allow_singular)
427
428 """
--> 429 dim, mean, cov = _process_parameters(None, mean, cov)
430 x = _process_quantiles(x, dim)
431 psd = _PSD(cov, allow_singular=allow_singular)
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/stats/_multivariate.pyc in _process_parameters(dim, mean, cov)
54
55 if mean.ndim != 1 or mean.shape[0] != dim:
---> 56 raise ValueError("Array 'mean' must be a vector of length %d." % dim)
57 if cov.ndim == 0:
58 cov = cov * np.eye(dim)
ValueError: Array 'mean' must be a vector of length 2.
ValueError声明数组“mean”必须是长度为2的向量,但实际情况是这样。实际上,平均值和协方差矩阵的维数以及传入的数据都是长度2。在