python-scipyHow do I fit a Gaussian distribution using Python and SciPy?
To fit a Gaussian distribution using Python and SciPy, you can use the scipy.stats.norm.fit()
function. This function takes an array of data samples and returns the mean and standard deviation of the best fitting Gaussian distribution.
Example code
from scipy.stats import norm
# Generate random data
data = np.random.randn(10000)
# Fit a normal distribution to the data
mu, std = norm.fit(data)
print('mu:', mu)
print('std:', std)
Output example
mu: 0.007817331827256067
std: 0.9908788996850786
The code above does the following:
- Imports the
norm
function from thescipy.stats
module. - Generates an array of random data using
np.random.randn()
. - Fits a normal distribution to the data using
norm.fit()
. - Prints the mean and standard deviation of the best fitting Gaussian distribution.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use the Python Scipy package?
- How do I use scipy.optimize.curve_fit in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python SciPy to calculate the Hilbert transform?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Scipy zeros in Python?
- How can I use Python Scipy to zoom in on an image?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
See more codes...