計數資料與 Poisson 分配

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

計數資料

  • 在特定的時間、距離、面積或體積單位內,計算「發生次數」

例子:

  • 一場足球比賽的進球數
  • 地震次數
  • 螃蟹攜帶衛星數
  • 一個人獲獎次數
  • 自行車通過橋樑的次數
Generalized Linear Models in Python

Poisson 隨機變數

  • 事件獨立且隨機發生
  • Poisson 分配

$$ P(y)=\frac{\lambda^ye^{-\lambda}}{y!} $$

  • $\lambda$:平均與變異數
  • $y=0,1,2,3, ...$
    • $\text{\color{#E80C7A}{永遠為正}}$
    • 離散(非連續)
    • $\text{\color{#E80C7A}{下界為 0}}$,無上界
Generalized Linear Models in Python

理解 Poisson 分配的參數

Poisson 隨機變數在 lambda 為 1、5、10 時的分配圖。

Generalized Linear Models in Python

視覺化迴應變數

import seaborn as sns
sns.distplot('y')
Generalized Linear Models in Python

Poisson 迴歸

  • 迴應變數 $$ y \sim Poisson(\lambda) $$

  • 迴應的期望值 $$ E(y)=\lambda $$

  • Poisson 迴歸模型 $$ log(\lambda)=\beta_0+\beta_1x_1 $$

Generalized Linear Models in Python

解釋變數

  • 連續與/或類別變數 → Poisson 迴歸模型
  • 類別變數 → 對數線性模型
Generalized Linear Models in Python

在 Python 使用 Poisson 的 GLM

import statsmodels.api as sm
from statsmodels.formula.api import glm
glm('y ~ x', 
    data = my_data,
    family = sm.families.Poisson())
Generalized Linear Models in Python

一起來練習吧!

Generalized Linear Models in Python

Preparing Video For Download...