【问题标题】:python- GLM Poisson Regression Probabilitiespython- GLM泊松回归概率
【发布时间】:2020-12-20 05:35:58
【问题描述】:

我正在使用 Statsmodel GLM 模型执行泊松回归。我有一个如下所示的数据集:

    Quantity  Month  cannibal_numbers  category_performance
0        0.0     11                 0                     7
1     3985.0      1                 1                     2
2     7690.0      2                 5                     4
3    10070.0      4                 3                    10

Quantity 是预测变量,其他 3 列是预测变量。

数量是预测变量,其他 3 列是预测变量。

按照 Statsmodels 文档,我以这种方式构建了泊松回归模型:


expr = """Quantity ~ Month  + cannibal_numbers + category_performance"""
        y, X = dmatrices(expr, series, return_type='dataframe')

poisson_fit = sm.GLM(y, X, family=sm.families.Poisson()).fit()

poisson_predict = poisson_fit.predict()

我被困在这里。我想要得到的是Quantity 将是 1、2、3 等的概率。直到 n。我不知道如何实现这一点。

如何在 statsmodels 中做到这一点?提前感谢您的任何指导 更新:感谢 Josef,事情变得更加清晰,按照他的建议调整了我的模型:

poisson_fit = sm.GLM(y, X, family=sm.families.Poisson()).fit()
    series['poisson_predict'] = poisson_fit.predict()


    counts = np.arange(4)
    predict_prob = stats.poisson.pmf(counts, np.asarray(series['poisson_predict'])[:, None])
    results = pd.DataFrame(predict_prob)

为数据集的每一行返回数量 = 1 到 4 的发生概率。如下:

              0             1             2             3             4   \
0   9.267928e-08  1.500859e-06  1.215255e-05  6.559995e-05  2.655834e-04   
1   9.267928e-08  1.500859e-06  1.215255e-05  6.559995e-05  2.655834e-04   
2   9.267928e-08  1.500859e-06  1.215255e-05  6.559995e-05  2.655834e-04   
3   2.286170e-07  3.495832e-06  2.672777e-05  1.362334e-04  5.207935e-04 
...

不会拟合模型给我这个数据(包括 mu)的方程线,因此可以通过考虑这个方程来预测 quantities 从 1 到 4 的发生概率,从而导致每个需求数量只有一个概率?

【问题讨论】:

    标签: python machine-learning statsmodels


    【解决方案1】:

    statsmodels.discrete 中的泊松模型在结果实例中有 predict_prob 方法来计算它。

    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/discrete/discrete_model.py#L3900

    对于泊松,我们可以直接使用 scipy.stats 分布,参数化是一样的。

    例如,使用 numpy 广播来获取行中所有预测案例的列中 0、... 4 的概率

    from scipy import stats
    poisson_predict = poisson_fit.predict()
    counts = np.arange(5)
    predict_prob = stats.poisson.pmf(counts, np.asarray(poisson_predict)[:, None])
    

    在其他一些 GLM 和计数分布(如负二项式)中,回归模型的参数化与 scipy.我们需要对参数进行转换,使其与 scipy.stats.distributions 参数化一致。

    一些较新的计数模型(如 GeneralizedPoisson 和零膨胀版本)在 predict 中有一个“which”选项,可以直接返回预测概率。

    例如对于 ZeroInflated 模型

    which str, optional
        Define values that will be predicted. 
        ‘mean’, ‘mean-main’, ‘linear’, ‘mean-nonzero’, 
        ‘prob-zero, ‘prob’, ‘prob-main’ Default is ‘mean’.
    

    【讨论】:

    • 非常感谢您的回答,这是无价的帮助!我仍然对一些事情感到困惑,为什么poisson_predict的结果在分发生成中被用作mu
    • 泊松分布的参数是mu,等于分布的均值。 predict 方法返回平均值的预测,因此对应于泊松参数 mu(或有时称为 lambda)
    • 谢谢 Josef,我还有很多东西要学,那为什么在你的例子中将 poisson predict 作为 mu,我觉得历史数据集的每个值都是 mu?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    相关资源
    最近更新 更多