Mukesh Gupta
Mukesh Gupta

Reputation: 1433

Pass 2-dimensional data in the Multivariate normal density function of python?

I want to calculate the Gaussian PDF of two dimensional data, i am trying to do this in python using scipy.stats.multivariate_normal function but i don't understand how can i pass my data into it?

Is multivariate_normal only used for analyzing one dimensional data in n-dimensions or can I use for my data set also?

data set-> X = [X1,X2....Xn] 

where each

Xi=[x1 x2] 

is 2 dimensional.

Upvotes: 2

Views: 4205

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114911

To compute the density function, use the pdf() method of the object scipy.stats.multivariate_normal. The first argument is your array X. The next two arguments are the mean and the covariance matrix of the distribution.

For example:

In [72]: import numpy as np

In [73]: from scipy.stats import multivariate_normal

In [74]: mean = np.array([0, 1])

In [75]: cov = np.array([[2, -0.5], [-0.5, 4]])

In [76]: x = np.array([[0, 1], [1, 1], [0.5, 0.25], [1, 2], [-1, 0]])

In [77]: x
Out[77]: 
array([[ 0.  ,  1.  ],
       [ 1.  ,  1.  ],
       [ 0.5 ,  0.25],
       [ 1.  ,  2.  ],
       [-1.  ,  0.  ]])

In [78]: p = multivariate_normal.pdf(x, mean, cov)

In [79]: p
Out[79]: array([ 0.05717014,  0.04416653,  0.05106649,  0.03639454,  0.03639454])

Upvotes: 3

Related Questions