Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

howto get fit parameters from seaborn distplot fit=?

Tags:

python

seaborn

I'm using seaborn distplot (data, fit=stats.gamma)

How do I get the fit parameters returned?

Here is an example:

import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
df = pd.read_csv ('RequestSize.csv')
import matplotlib.pyplot as plt
reqs = df['12 web pages']
reqs = reqs.dropna()
reqs = reqs[np.logical_and (reqs > np.percentile (reqs, 0), reqs < np.percentile (reqs, 95))]
dist = sns.distplot (reqs, fit=stats.gamma)
like image 560
nbecker Avatar asked Jul 14 '15 17:07

nbecker


People also ask

What does Distplot () mean in SNS Distplot ()?

distplot() function accepts the data variable as an argument and returns the plot with the density distribution. Example 1: import numpy as np import seaborn as sn import matplotlib.

What is the difference between Distplot and Displot?

displot() is the new distplot() with better capabilities and distplot() is deprecated starting from this Seaborn version. With the new displot() function in Seaborn, the plotting function hierarchy kind of of looks like this now covering most of the plotting capabilities.

What is KDE in Seaborn Distplot?

A kernel density estimate (KDE) plot is a method for visualizing the distribution of observations in a dataset, analogous to a histogram. KDE represents the data using a continuous probability density curve in one or more dimensions.

What is Hist_kws?

hist_kws is an optional argument in distplot which takes only values in a dictionary. You can use this to set linewidth, edgecolor etc. Example for your ref. Follow this answer to receive notifications. answered May 30, 2021 at 16:19.


2 Answers

Use the object you passed to distplot:

stats.gamma.fit(reqs)
like image 145
mwaskom Avatar answered Oct 02 '22 12:10

mwaskom


I confirm the above is true - the sns.distplot fit method is equivalent to the fit method in scipy.stats so you can get the parameters from there, e.g.:

from scipy import stats

ax = sns.distplot(e_t_hat, bins=20, kde=False, fit=stats.norm);
plt.title('Distribution of Cointegrating Spread for Brent and Gasoil')

# Get the fitted parameters used by sns
(mu, sigma) = stats.norm.fit(e_t_hat)
print "mu={0}, sigma={1}".format(mu, sigma)

# Legend and labels 
plt.legend(["normal dist. fit ($\mu=${0:.2g}, $\sigma=${1:.2f})".format(mu, sigma)])
plt.ylabel('Frequency')

# Cross-check this is indeed the case - should be overlaid over black curve
x_dummy = np.linspace(stats.norm.ppf(0.01), stats.norm.ppf(0.99), 100)
ax.plot(x_dummy, stats.norm.pdf(x_dummy, mu, sigma))
plt.legend(["normal dist. fit ($\mu=${0:.2g}, $\sigma=${1:.2f})".format(mu, sigma),
           "cross-check"])

enter image description here

like image 33
tsando Avatar answered Oct 02 '22 13:10

tsando