试图绘制双变量法的等值线图,将不会有相关项。

时间:2021-10-10 01:00:02

refer to this tutorial: http://matplotlib.org/1.4.0/examples/pylab_examples/contour_demo.html

参考本教程:http://matplotlib.org/1.4.0/examples/pylab_examples/contour_demo.html。

Here is the prototype for the bivariate_normal function from mplotlib.mlab:

这是来自mplotlib.mlab的bivariate_normal函数的原型:

bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0, mux=0.0, muy=0.0, sigmaxy=0.0)

X and Y define the grid, and we have arguments for the 2 dimensional means and covariance terms. As you can see, there is an argument at the end for a the covariance between x and y. Here's the thing: plt.contour() will plot bivariate normal contours if sigmaxy = 0. However, if sigmaxy has any other value, I get a

X和Y定义了网格,我们有关于二维均值和协方差的参数。正如你所看到的,在x和y之间的协方差是有一个参数的,这里是,如果sigmaxy = 0,那么,plt.contour()会绘制双变量的正等值线。但是,如果sigmaxy有其他的值,我就得到a。

ValueError: zero-size array to reduction operation minimum which has no identity

For example,

例如,

Z =  bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0, 0.0)
plt.contour(X,Y,Z)

works

作品

But, the following does not work:

但是,下面的方法不起作用:

Z = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0, 1.0)
plt.contour(X,Y,Z)

Anyone familiar with matplotlib have any ideas? Thanks.

熟悉matplotlib的人有什么想法吗?谢谢。

2 个解决方案

#1


1  

It does not work because your covariance matrix is not positive definite. To see if your matrix is positive definite you can check whether all its eigenvalues are greater than zero.

它不起作用,因为你的协方差矩阵不是正的。看看你的矩阵是否为正,你可以检查它的所有特征值是否大于零。

Extreme case

极端的例子

import numpy as np
from matplotlib.mlab import bivariate_normal
from matplotlib import pylab as plt


cov_test = np.array([[1,0.999],
                     [0.999,1]])

print np.linalg.eigvals(cov_test)

[ 1.99900000e+00 1.00000000e-03]

(1.99900000 e + 00 1.00000000 e 03)

You can see the second eigenvalue is super close to zero. Actually, if you plot it, you see this is really an extreme case of covariance:

你可以看到第二个特征值非常接近于零。实际上,如果你把它画出来,你会发现这是一个极端的协方差例子:

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 = bivariate_normal(X, Y, cov_test[0,0], cov_test[1,1], 0.0, 0.0, cov_test[0,1])
plt.contour(X,Y,Z)

试图绘制双变量法的等值线图,将不会有相关项。

Non positive definite case:

非正定的例子:

And if you go a bit further...

如果你更进一步…

import numpy as np
from matplotlib import pylab as plt

cov_test = np.array([[1,1],
                 [1,1]])

print np.linalg.eigvals(cov_test)

[ 2. 0.]

(2。0。)

Then the second eigenvalue reaches 0, this is not positive definite and if you try to plot it then:

第二个特征值是0,这是不确定的,如果你想画出来

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 = bivariate_normal(X, Y, cov_test[0,0], cov_test[1,1], 0.0, 0.0, cov_test[0,1])
plt.contour(X,Y,Z)

You get the error:

你得到了错误:

ValueError: zero-size array to reduction operation minimum which has no identity`

Actually, my Z is now full of NaN

事实上,我的Z现在充满了NaN。

#2


3  

To take the notation from the accepted answer:

从被接受的答案中提取符号:

cov_test = np.array([[..., ...],
                     [..., ...]])

You need to pass the standard deviation (in the arguments sigmax and sigmay), not the variance:

您需要传递标准偏差(在参数sigmax和sigmay中),而不是方差:

Z = bivariate_normal(X, Y, np.sqrt(cov_test[0,0]), np.sqrt(cov_test[1,1]),
                     0.0, 0.0, cov_test[0,1])

For whatever reason, the sigmaxy argument is the actual entry from the covariance matrix (actually rho*sigmax*sigmay).

无论出于什么原因,sigmaxy参数是来自协方差矩阵的实际条目(实际上是*sigmax*sigmay)。

For any nontrivial example, the code will blow up when it tries to calculate rho if you try to pass in either all covariance or all sqrt covariance.

对于任何非平凡的例子,如果试图通过所有协方差或所有sqrt协方差来计算,代码将会被放大。

#1


1  

It does not work because your covariance matrix is not positive definite. To see if your matrix is positive definite you can check whether all its eigenvalues are greater than zero.

它不起作用,因为你的协方差矩阵不是正的。看看你的矩阵是否为正,你可以检查它的所有特征值是否大于零。

Extreme case

极端的例子

import numpy as np
from matplotlib.mlab import bivariate_normal
from matplotlib import pylab as plt


cov_test = np.array([[1,0.999],
                     [0.999,1]])

print np.linalg.eigvals(cov_test)

[ 1.99900000e+00 1.00000000e-03]

(1.99900000 e + 00 1.00000000 e 03)

You can see the second eigenvalue is super close to zero. Actually, if you plot it, you see this is really an extreme case of covariance:

你可以看到第二个特征值非常接近于零。实际上,如果你把它画出来,你会发现这是一个极端的协方差例子:

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 = bivariate_normal(X, Y, cov_test[0,0], cov_test[1,1], 0.0, 0.0, cov_test[0,1])
plt.contour(X,Y,Z)

试图绘制双变量法的等值线图,将不会有相关项。

Non positive definite case:

非正定的例子:

And if you go a bit further...

如果你更进一步…

import numpy as np
from matplotlib import pylab as plt

cov_test = np.array([[1,1],
                 [1,1]])

print np.linalg.eigvals(cov_test)

[ 2. 0.]

(2。0。)

Then the second eigenvalue reaches 0, this is not positive definite and if you try to plot it then:

第二个特征值是0,这是不确定的,如果你想画出来

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 = bivariate_normal(X, Y, cov_test[0,0], cov_test[1,1], 0.0, 0.0, cov_test[0,1])
plt.contour(X,Y,Z)

You get the error:

你得到了错误:

ValueError: zero-size array to reduction operation minimum which has no identity`

Actually, my Z is now full of NaN

事实上,我的Z现在充满了NaN。

#2


3  

To take the notation from the accepted answer:

从被接受的答案中提取符号:

cov_test = np.array([[..., ...],
                     [..., ...]])

You need to pass the standard deviation (in the arguments sigmax and sigmay), not the variance:

您需要传递标准偏差(在参数sigmax和sigmay中),而不是方差:

Z = bivariate_normal(X, Y, np.sqrt(cov_test[0,0]), np.sqrt(cov_test[1,1]),
                     0.0, 0.0, cov_test[0,1])

For whatever reason, the sigmaxy argument is the actual entry from the covariance matrix (actually rho*sigmax*sigmay).

无论出于什么原因,sigmaxy参数是来自协方差矩阵的实际条目(实际上是*sigmax*sigmay)。

For any nontrivial example, the code will blow up when it tries to calculate rho if you try to pass in either all covariance or all sqrt covariance.

对于任何非平凡的例子,如果试图通过所有协方差或所有sqrt协方差来计算,代码将会被放大。