python-scipyHow can I use Python Scipy to create a Gaussian Kernel Density Estimation?
To use Python Scipy to create a Gaussian Kernel Density Estimation, you will need to use the scipy.stats.gaussian_kde
class. This class takes in a dataset and creates an estimation of the probability density function of the given dataset.
Example code
import scipy.stats
import numpy as np
# Generate a random sample of data points
data = np.random.randn(1000)
# Create a Gaussian Kernel Density Estimation
kde = scipy.stats.gaussian_kde(data)
# Evaluate the estimated probability density function
print(kde.evaluate(data))
Output example
[0.00232093 0.00252935 0.00222411 ... 0.00221902 0.00221639 0.00221902]
The code does the following:
- Imports the
scipy.stats
andnumpy
libraries. - Generates a random sample of data points using the
np.random.randn()
function. - Creates a Gaussian Kernel Density Estimation using the
scipy.stats.gaussian_kde()
class. - Evaluates the estimated probability density function using the
kde.evaluate()
method.
Helpful links
More of Python Scipy
- How can I check if a certain version of Python is compatible with SciPy?
- How do I rotate an image using Python and SciPy?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
- How do I integrate Scipy with Python?
- How do I use Scipy zeros in Python?
- How do I use Python Scipy to perform a Z test?
- How do I check the version of Python SciPy I'm using?
- How do I use the numpy vstack function in Python?
- How can I use python scipy to calculate the norm of a vector?
- How do I calculate a Jacobian matrix using Python and NumPy?
See more codes...