scipy - How to fit a poisson distribution with seaborn? -
i try fit data poisson distribution:
import seaborn sns import scipy.stats stats sns.distplot(x, kde = false, fit = stats.poisson)
but error:
attributeerror: 'poisson_gen' object has no attribute 'fit'
other distribution (gamma, etc) de work well.
the poisson distribution (implemented in scipy scipy.stats.poisson
) discrete distribution. discrete distributions in scipy not have fit
method.
i'm not familiar seaborn.distplot
function, appears assume data comes continuous distribution. if case, if scipy.stats.poisson
had fit
method, not appropriate distribution pass distplot
.
the question title "how fit poisson distribution seaborn?", sake of completeness, here's 1 way plot of data , fit. seaborn
used bar plot, using @mwaskom's suggestion use seaborn.countplot
. fitting trivial, because maximum likelihood estimation poisson distribution mean of data.
first, imports:
in [136]: import numpy np in [137]: scipy.stats import poisson in [138]: import matplotlib.pyplot plt in [139]: import seaborn
generate data work with:
in [140]: x = poisson.rvs(0.4, size=100)
these values in x
:
in [141]: k = np.arange(x.max()+1) in [142]: k out[142]: array([0, 1, 2, 3])
use seaborn.countplot
plot data:
in [143]: seaborn.countplot(x, order=k, color='g', alpha=0.5) out[143]: <matplotlib.axes._subplots.axessubplot @ 0x114700490>
the maximum likelihood estimation of poisson parameter mean of data:
in [144]: mlest = x.mean()
use poisson.pmf()
expected probability, , multiply size of data set expected counts, , plot using matplotlib
. bars counts of actual data, , dots expected counts of fitted distribution:
in [145]: plt.plot(k, poisson.pmf(k, mlest)*len(x), 'go', markersize=9) out[145]: [<matplotlib.lines.line2d @ 0x114da74d0>]
Comments
Post a Comment