简体   繁体   中英

python multivariate normal pdf 3d plot

I am trying to show a 3d-plot of a multivariat normal pdf for all zeros number in the mnist dataset.

from scipy.stats import multivariate_normal
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

my0 = np.mean(num_arrays[0],axis=0)
sigma0 = np.identity(784)
p0 = multivariate_normal(my0,sigma0)

X, Y = np.mgrid[-10:10:.1, -10:10:.1]
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, p0_id.pdf(pos),cmap='viridis',linewidth=0)

I get the following error message:

operands could not be broadcast together with shapes (200,200,2) (784,)

What am I doing wrong here?

edit: full error message

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-70-584e158fe420> in <module>()
     13 fig = plt.figure()
     14 ax = fig.gca(projection='3d')
---> 15 ax.plot_surface(X, Y, p0.pdf(pos),cmap='viridis',linewidth=0)

~\Anaconda3\lib\site-packages\scipy\stats\_multivariate.py in pdf(self, x)
    608 
    609     def pdf(self, x):
--> 610         return np.exp(self.logpdf(x))
    611 
    612     def rvs(self, size=1, random_state=None):

~\Anaconda3\lib\site-packages\scipy\stats\_multivariate.py in logpdf(self, x)
    604         x = self._dist._process_quantiles(x, self.dim)
    605         out = self._dist._logpdf(x, self.mean, self.cov_info.U,
--> 606                                  self.cov_info.log_pdet, self.cov_info.rank)
    607         return _squeeze_output(out)
    608 

~\Anaconda3\lib\site-packages\scipy\stats\_multivariate.py in _logpdf(self, x, mean, prec_U, log_det_cov, rank)
    452 
    453         """
--> 454         dev = x - mean
    455         maha = np.sum(np.square(np.dot(dev, prec_U)), axis=-1)
    456         return -0.5 * (rank * _LOG_2PI + log_det_cov + maha)

ValueError: operands could not be broadcast together with shapes (200,200,2) (784,) 

I'm trying to do somewhat the same stuff, and the only think I found to have an idea of the original shape it to compute a point to point result of the function and to plot this point with the Axes3D.scatter() function, here is an example of how I get the shape of a 3D gaussian with python

import numpy as np
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D
from scipy.stats import multivariate_normal
mean = np.array([1., 1.])
cov_matrix = np.array([[2., 0.], [0., 2.]])
fig = plt.figure()
ax = fig.gca(projection="3d")
x = np.linspace(-3., 3., 20)
y = np.linspace(-3., 3., 20)
for i in x:
    for j in y:
        ax.scatter(i, j, pdf_2d(i, j,  multivariate_normal.pdf([i, j], mean=mean_, cov=cov_matrix_))
plt.show()

As meshgrid produce an multidimensional array, I think that the pdf() function is not able to apply vectorial transformation to each element..

That's unfortunately the only way I found to have an idea of the gaussian's shape in my case..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM