最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何用seaborn拟合泊松分布?

SEO心得admin251浏览0评论
本文介绍了如何用seaborn拟合泊松分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试将我的数据拟合为泊松分布:

导入 seaborn 为 sns导入 scipy.stats 作为统计信息sns.distplot(x, kde = False, fit = stats.poisson)

但我收到此错误:

AttributeError: 'poisson_gen' 对象没有属性 'fit'

其他分布(伽马等)运行良好.

解决方案

I try to fit my data to a poisson distribution:

import seaborn as sns import scipy.stats as stats sns.distplot(x, kde = False, fit = stats.poisson)

But I get this error:

AttributeError: 'poisson_gen' object has no attribute 'fit'

Other distribution (gamma, etc) de work well.

解决方案

The Poisson distribution (implemented in scipy as scipy.stats.poisson) is a discrete distribution. The discrete distributions in scipy do not have a fit method.

I'm not very familiar with the seaborn.distplot function, but it appears to assume that the data comes from a continuous distribution. If that is the case, then even if scipy.stats.poisson had a fit method, it would not be an appropriate distribution to pass to distplot.

The question title is "How to fit a poisson distribution with seaborn?", so for the sake of completeness, here's one way to get a plot of the data and its fit. seaborn is only used for the bar plot, using @mwaskom's suggestion to use seaborn.countplot. The fitting is actually trivial, because the maximum likelihood estimation for the Poisson distribution is simply the mean of the data.

First, the imports:

In [136]: import numpy as np In [137]: from scipy.stats import poisson In [138]: import matplotlib.pyplot as plt In [139]: import seaborn

Generate some data to work with:

In [140]: x = poisson.rvs(0.4, size=100)

These are the values in the x:

In [141]: k = np.arange(x.max()+1) In [142]: k Out[142]: array([0, 1, 2, 3])

Use seaborn.countplot to plot the data:

In [143]: seaborn.countplot(x, order=k, color='g', alpha=0.5) Out[143]: <matplotlib.axes._subplots.AxesSubplot at 0x114700490>

The maximum likelihood estimation of the Poisson parameter is simply the mean of the data:

In [144]: mlest = x.mean()

Use poisson.pmf() to get the expected probability, and multiply by the size of the data set to get the expected counts, and then plot using matplotlib. The bars are the counts of the actual data, and the dots are the expected counts of the fitted distribution:

In [145]: plt.plot(k, poisson.pmf(k, mlest)*len(x), 'go', markersize=9) Out[145]: [<matplotlib.lines.Line2D at 0x114da74d0>]

发布评论

评论列表(0)

  1. 暂无评论