Python में GLM कैसे फिट करें?

Python में Generalized Linear Models

Ita Cirovic Donev

Data Science Consultant

statsmodels

  • statsmodels इम्पोर्ट करना

    import statsmodels.api as sm
    
  • फॉर्मूला सपोर्ट

    import statsmodels.formula.api as smf
    
  • सीधे glm() इस्तेमाल करें

    from statsmodels.formula.api import glm
    
Python में Generalized Linear Models

Model fit की प्रक्रिया

  1. मॉडल बताइए $\rightarrow \texttt{\color{#007AFF}{glm()}}$
  2. मॉडल फिट कीजिए $\rightarrow \texttt{\color{#007AFF}{.fit()}}$
  3. मॉडल का सार देखें $\rightarrow \texttt{\color{#007AFF}{.summary()}}$
  4. प्रेडिक्शन बनाइए $\rightarrow \texttt{\color{#007AFF}{.predict()}}$
Python में Generalized Linear Models

मॉडल का वर्णन

FORMULA आधारित

from statsmodels.formula.api import glm
model = glm(formula, data, family)

ARRAY आधारित

import statsmodels.api as sm
X = sm.add_constant(X)
model = sm.glm(y, X, family)
Python में Generalized Linear Models

Formula आर्गुमेंट

$$\texttt{\color{#00A388}{response}} \sim \texttt{\color{#FF6138}{explanatory variable(s)}}$$ $$\texttt{\color{#00A388}{output}} \sim \texttt{\color{#FF6138}{input(s)}}$$

formula = 'y ~ x1 + x2'
  • $\texttt{\color{#FF6138}{C(x1)}}$ : x1 को श्रेणीबद्ध वैरिएबल मानें
  • $\texttt{\color{#FF6138}{-1}}$ : इंटरसेप्ट हटाएँ
  • $\texttt{\color{#FF6138}{x1:x2}}$ : x1 और x2 के बीच इंटरेक्शन टर्म
  • $\texttt{\color{#FF6138}{x1*x2}}$ : x1-x2 का इंटरेक्शन और दोनों अलग-अलग वैरिएबल्स
  • $\texttt{\color{#FF6138}{np.log(x1)}}$ : मॉडल वैरिएबल्स पर वेक्टराइज़्ड फंक्शन लगाएँ
Python में Generalized Linear Models

Family आर्गुमेंट

family = sm.families.____()

फैमिली फंक्शंस:

  • $\texttt{\color{#007AFF}{Gaussian}(link = sm.families.links.\color{deeppink}{identity()})}$ $\rightarrow$ डिफॉल्ट फैमिली
  • $\texttt{\color{#007AFF}{Binomial}(link = sm.families.links.\color{deeppink}{logit()})}$
    • $\texttt{\color{deeppink}{probit()}}$, $\texttt{\color{deeppink}{cauchy()}}$, $\texttt{\color{deeppink}{log()}}$, और $\texttt{\color{deeppink}{cloglog()}}$
  • $\texttt{\color{#007AFF}{Poisson}(link = sm.families.links.\color{deeppink}{log()})}$
    • $\texttt{\color{deeppink}{identity()}}$ और $\texttt{\color{deeppink}{sqrt()}}$

अन्य डिस्ट्रीब्यूशन फैमिलीज़ आप statsmodels website पर देख सकते हैं.

Python में Generalized Linear Models

मॉडल का सारांश

print(model_GLM.summary())
Python में Generalized Linear Models
                 Generalized Linear Model Regression Results                  
=============================================================================
Dep. Variable:                      y  No. Observations:                  173
Model:                            GLM  Df Residuals:                      171
Model Family:                Binomial  Df Model:                            1
Link Function:                  logit  Scale:                          1.0000
Method:                          IRLS  Log-Likelihood:                -97.226
Date:                Mon, 21 Jan 2019  Deviance:                       194.45
Time:                        11:30:01  Pearson chi2:                     165.
No. Iterations:                     4  Covariance Type:             nonrobust
=============================================================================
                 coef    std err         z      P>|z|      [0.025      0.975]
-----------------------------------------------------------------------------
Intercept    -12.3508      2.629    -4.698      0.000     -17.503      -7.199
width          0.4972      0.102     4.887      0.000       0.298       0.697
=============================================================================
Python में Generalized Linear Models

Regression coefficients

$\texttt{\color{#007AFF}{.params}}$ रिग्रेशन कोएफ़िशिएंट्स दिखाता है

model_GLM.params
Intercept   -12.350818
width         0.497231
dtype: float64

$\texttt{\color{#007AFF}{.conf\_int(alpha=0.05, cols=None)}}$ कॉन्फिडेंस इंटरवल्स दिखाता है

model_GLM.conf_int()
                   0         1
Intercept -17.503010 -7.198625
width       0.297833  0.696629
Python में Generalized Linear Models

Predictions

  • टेस्ट डेटा में सभी मॉडल वैरिएबल्स दें
  • $\texttt{\color{#007AFF}{.predict(\color{#FF931B}{test\_data})}}$ प्रेडिक्शन निकालता है
model_GLM.predict(test_data)
0    0.029309
1    0.470299
2    0.834983
3    0.972363
4    0.987941
Python में Generalized Linear Models

अभ्यास करते हैं!

Python में Generalized Linear Models

Preparing Video For Download...