【发布时间】: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