我有以下正常分布点:
import numpy as np
from matplotlib import pylab as plt
from matplotlib import mlab
mean_test = np.array([0,0])
cov_test = array([[ 0.6744121 , -0.16938146],
[-0.16938146, 0.21243464]])
协方差矩阵是确定的半正,因此它可以用作协方差
# Semi-positive definite if all eigenvalues are 0 or
# if there exists a Cholesky decomposition
print np.linalg.eigvals(cov_test)
print np.linalg.cholesky(cov_test)
[0.72985988 0.15698686]
[[0.82122597 0.] [-0.20625439 0.41218172]]
如果我得到一些积分,我会得到:
data_test = np.random.multivariate_normal(mean_test, cov_test, 1000)
plt.scatter(data_test[:,0],data_test[:,1])
问题:
当我尝试绘制协方差轮廓时,为什么bivariate_normal
方法失败(返回NaNs)?
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = mlab.bivariate_normal(X, Y,
cov_test[0,0], cov_test[1,1],
0, 0, cov_test[0,1])
print Z
plt.contour(X, Y, Z)
输出:
[[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
...,
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]
[ nan nan nan ..., nan nan nan]]
ValueError: zero-size array to reduction operation minimum which has no identity
答案 0 :(得分:5)
协方差矩阵的对角线是方差,但sigmax
的参数sigmay
和mlab.bivariate_normal
是方差的平方根。改变这个:
Z = mlab.bivariate_normal(X, Y,
cov_test[0,0], cov_test[1,1],
0, 0, cov_test[0,1])
到此:
Z = mlab.bivariate_normal(X, Y,
np.sqrt(cov_test[0,0]), np.sqrt(cov_test[1,1]),
0, 0, cov_test[0,1])