การคำนวณและอธิบายค่าพยากรณ์

Generalized Linear Models ใน Python

Ita Cirovic Donev

Data Science Consultant

การคำนวณค่าพยากรณ์

หลังจากได้รับผลการ fit โมเดล

  1. ค่า fitted สำหรับค่า $x$ เดิม

ไดอะแกรมแสดงกระบวนการ fit โมเดล

Generalized Linear Models ใน Python

การคำนวณค่าพยากรณ์

หลังจากได้รับผลการ fit โมเดล

  1. ค่า fitted สำหรับค่า $x$ เดิม

  2. ค่า $x$ ใหม่ สำหรับ ค่าพยากรณ์

ไดอะแกรมแสดงกระบวนการ fit โมเดลและการคำนวณค่าพยากรณ์

Generalized Linear Models ใน Python

การคำนวณค่าพยากรณ์

  • โมเดลปูเกือกม้า y ~ weight $$ \mu = \frac{\exp(-3.6947+1.8151 \times weight)}{1+\exp(-3.6947+1.8151 \times weight)} $$

  • การวัดค่าใหม่: weight = 2.85

$$ \mu = \frac{\exp(-3.6947+1.8151 \times \color{blue}{2.85})}{1+\exp(-3.6947+1.8151 \times \color{blue}{2.85})} = 0.814 $$

Generalized Linear Models ใน Python

การพยากรณ์ใน Python

  • คำนวณค่าพยากรณ์ของโมเดลสำหรับชุดข้อมูล new_data
    # Compute model predictions
    model_GLM.predict(exog = new_data)
    
Generalized Linear Models ใน Python

จากความน่าจะเป็นสู่คลาส

การแยกผลลัพธ์ของโมเดลตามค่าตัดขาดความน่าจะเป็นที่กำหนด

Generalized Linear Models ใน Python

การคำนวณค่าพยากรณ์ระดับคลาส

# Extract fitted probabilities from model
crab['fitted'] = model.fittedvalues.values
# Define cut-off value
cut_off = 0.4
# Compute class predictions
crab['pred_class'] = np.where(crab['fitted'] > cut_off, 1, 0)
Generalized Linear Models ใน Python

การคำนวณค่าพยากรณ์ระดับคลาส

# Count occurences for each class
crab['pred_class'].value_counts()
1    151
0     22
ค่าตัดขาด $\hat y=1$ $\hat y=0$
$\mu = 0.4$ 151 22
$\mu = 0.5$ 126 47
Generalized Linear Models ใน Python

Confusion matrix

Confusion matrix เปล่า

Generalized Linear Models ใน Python

Confusion matrix - True Negatives

การแสดง True Negatives ใน confusion matrix

Generalized Linear Models ใน Python

Confusion matrix - True Positives

การแสดง True Negatives และ True Positives ใน confusion matrix

Generalized Linear Models ใน Python

Confusion matrix - False Positives

การแสดง True Negatives, True Positives และ False Positives ใน confusion matrix

Generalized Linear Models ใน Python

Confusion matrix - False Negatives

การแสดง True Negatives, True Positives, False Positives และ False Negatives ใน confusion matrix

Generalized Linear Models ใน Python

Confusion matrix ใน Python

print(pd.crosstab(y_actual, y_predicted, 
            rownames=['Actual'], colnames=['Predicted'], 
            margins = True))
Predicted   0    1  All
Actual                 
0          15   47   62
1           7  104  111
All        22  151  173
Generalized Linear Models ใน Python

มาฝึกกันเถอะ!

Generalized Linear Models ใน Python

Preparing Video For Download...