मॉडल फ़ॉर्मूला

Python में Generalized Linear Models

Ita Cirovic Donev

Data Science Consultant

फ़ॉर्मूला और मॉडल मैट्रिक्स

डेटा सोर्स X और Y के साथ डायग्राम की शुरुआत.

Python में Generalized Linear Models

फ़ॉर्मूला और मॉडल मैट्रिक्स

फ़ॉर्मूला डायग्राम

Python में Generalized Linear Models

फ़ॉर्मूला और मॉडल मैट्रिक्स

मॉडल मैट्रिक्स डायग्राम

Python में Generalized Linear Models

फ़ॉर्मूला और मॉडल मैट्रिक्स

glm क्लास के इनपुट का डायग्राम.

Python में Generalized Linear Models

मॉडल मैट्रिक्स

  • मॉडल मैट्रिक्स: $y \sim \bold{X}$

  • मॉडल फ़ॉर्मूला

    'y ~ x1 + x2'
    
  • मॉडल मैट्रिक्स की संरचना जाँचें
    from patsy import dmatrix
    dmatrix('x1 + x2')
    
  Intercept  x1  x2
          1   1   4
          1   2   5
          1   3   6
Python में Generalized Linear Models

वैरिएबल ट्रांसफॉर्मेशन

import numpy as np
'y ~ x1 + np.log(x2)'
dmatrix('x1 + np.log(x2)')
DesignMatrix with shape (3, 3)
  Intercept  x1  np.log(x2)
          1   1     1.38629
          1   2     1.60944
          1   3     1.79176
Python में Generalized Linear Models

सेंट्रिंग और स्टैंडर्डाइजेशन

  • Stateful ट्रांसफॉर्म्स
'y ~ center(x1) + standardize(x2)'
dmatrix('center(x1) + standardize(x2)')
DesignMatrix with shape (3, 3)
  Intercept  center(x1)  standardize(x2)
          1          -1         -1.22474
          1           0          0.00000
          1           1          1.22474
Python में Generalized Linear Models

अपना ट्रांसफॉर्मेशन बनाएँ

def my_transformation(x):
  return 4 * x
dmatrix('x1 + x2 + my_transformation(x2)')
DesignMatrix with shape (3, 4)
  Intercept  x1  x2  my_transformation(x2)
          1   1   4                     16
          1   2   5                     20
          1   3   6                     24
Python में Generalized Linear Models

अंकगणितीय ऑपरेशन

x1 = np.array([1, 2, 3])
x2 = np.array([4,5,6])

dmatrix('I(x1 + x2'))
DesignMatrix with shape (3, 2)
  Intercept  I(x1 + x2)
          1           5
          1           7
          1           9
x1 = [1, 2, 3]
x2 = [4,5,6]

dmatrix('I(x1 + x2)')
DesignMatrix with shape (6, 2)
  Intercept  I(x1 + x2)
          1           1
          1           2
          1           3
          1           4
          1           5
          1           6
Python में Generalized Linear Models

Categorical डेटा की कोडिंग

रंग प्रकार: लाल, हरा, नीला

Python में Generalized Linear Models

Categorical डेटा की कोडिंग

रंग प्रकार: लाल, हरा, नीला और डेटा में रंग ऑब्ज़र्वेशंस का डायग्राम

Python में Generalized Linear Models

Categorical डेटा की कोडिंग

लाल, हरा और नीला रंग का उपयोग करते हुए वन-हॉट एनकोडिंग का डायग्राम.

Python में Generalized Linear Models

Patsy कोडिंग

  • स्ट्रिंग और बूलियन अपने-आप कोड होते हैं
  • न्यूमेरिक $\rightarrow$ कैटेगोरिकल
    • C() फंक्शन
  • रेफ़रेंस ग्रुप
    • डिफ़ॉल्ट: पहला ग्रुप
    • Treatment
    • levels
Python में Generalized Linear Models

C() फंक्शन

  • न्यूमेरिक वैरिएबल
    dmatrix('color', data = crab)
    
DesignMatrix with shape (173, 2)
  Intercept  color
          1      2
          1      3
          1      1
  [... rows omitted]
  • कितने लेवल हैं?
    crab['color'].value_counts()
    
2    95
3    44
4    22
1    12
Python में Generalized Linear Models

C() फंक्शन

  • कैटेगोरिकल वैरिएबल
    dmatrix('C(color)', data = crab)
    
DesignMatrix with shape (173, 4)
  Intercept  C(color)[T.2]  C(color)[T.3]  C(color)[T.4]
          1              1              0              0
          1              0              1              0
          1              0              0              0
  [... rows omitted]
Python में Generalized Linear Models

रेफ़रेंस ग्रुप बदलना

dmatrix('C(color, Treatment(4))', data = crab)
DesignMatrix with shape (173, 4)
  Intercept  C(color)[T.1]  C(color)[T.2]  C(color)[T.3]  
          1              0              1              0 
          1              0              0              1 
          1              1              0              0 
  [... rows omitted]
Python में Generalized Linear Models

रेफ़रेंस ग्रुप बदलना

l = [1, 2, 3,4]
dmatrix('C(color, levels = l)', data = crab)
DesignMatrix with shape (173, 4)
  Intercept  C(color)[T.2]  C(color)[T.3]   C(color)[T.4] 
          1               1            0               0     
          1               0            1               0
          1               0            0               0 
  [... rows omitted]
Python में Generalized Linear Models

मल्टिपल इंटरसेप्ट्स

'y ~ C(color)-1'
dmatrix('C(color)-1', data = crab)
DesignMatrix with shape (173, 4)
  C(color)[1]  C(color)[2]  C(color)[3]  C(color)[4]
            0            1            0            0
            0            0            1            0
            1            0            0            0
  [... rows omitted]
Python में Generalized Linear Models

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

Python में Generalized Linear Models

Preparing Video For Download...