การพล็อตโมเดลการถดถอย

Generalized Linear Models ใน Python

Ita Cirovic Donev

Data Science Consultant

นำเข้าไลบรารี

import seaborn as sns
import matplotlib.pyplot as plt
  • โมเดลปู 'sat ~ width' ถูกบันทึกไว้ในชื่อ model
Generalized Linear Models ใน Python

พล็อตจุดข้อมูล

# Adjust figure size
plt.subplots(figsize = (8, 5))
# Plot data points
sns.regplot('width', 'sat', 
            data = crab,
            fit_reg = False)

Scatterplot ของความกว้างและจำนวนดาวเทียมจากชุดข้อมูลปู

Generalized Linear Models ใน Python

เพิ่ม jitter

sns.regplot('width', 'sat', 
            data = crab,
            fit_reg = False,
            y_jitter = 0.3)

Scatterplot ที่เพิ่ม jitter ของความกว้างและจำนวนดาวเทียมจากชุดข้อมูลปู

Generalized Linear Models ใน Python

เพิ่ม linear fit

sns.regplot('width', 'sat', 
            data = crab,
            y_jitter = 0.3,
            fit_reg = True,
            line_kws = {'color':'green', 
                        'label':'LM fit'})

เส้น linear fit พร้อม confidence interval และ scatterplot ของความกว้างและจำนวนดาวเทียมจากชุดข้อมูลปู

Generalized Linear Models ใน Python

เพิ่มค่าประมาณจาก Poisson GLM

crab['fit_values'] = model.fittedvalues
sns.scatterplot('width','fit_values', 
                data = crab,
                color = 'red', 
                label = 'Poisson')

Linear fit และ Poisson fit พร้อม scatterplot ของความกว้างและจำนวนดาวเทียมจากชุดข้อมูลปู

Generalized Linear Models ใน Python

การพยากรณ์

เส้น Poisson model fit ซ้อนทับบน scatterplot ของความกว้างและจำนวนดาวเทียม

Generalized Linear Models ใน Python

การพยากรณ์

new_data = pd.DataFrame({'width':[24, 28, 32]})
model.predict(new_data)
0    1.881981

การอ่านค่าพยากรณ์ที่ความกว้าง 24 ซม. จากโมเดล Poisson regression ที่ fit แล้ว

Generalized Linear Models ใน Python

การพยากรณ์

new_data = pd.DataFrame({'width':[24, 28, 32]})
model.predict(new_data)
0    1.881981
1    3.627360

การอ่านค่าพยากรณ์ที่ความกว้าง 28 ซม. จากโมเดล Poisson regression ที่ fit แล้ว

Generalized Linear Models ใน Python

การพยากรณ์

new_data = pd.DataFrame({'width':[24, 28, 32]})
model.predict(new_data)
0    1.881981
1    3.627360
2    6.991433

การอ่านค่าพยากรณ์ที่ความกว้าง 32 ซม. จากโมเดล Poisson regression ที่ fit แล้ว

Generalized Linear Models ใน Python

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

Generalized Linear Models ใน Python

Preparing Video For Download...