Menghitung dan mendeskripsikan prediksi

Generalized Linear Models di Python

Ita Cirovic Donev

Data Science Consultant

Menghitung prediksi

Setelah model di-fit

  1. Nilai fit untuk $x$ asli

Diagram proses pemodelan.

Generalized Linear Models di Python

Menghitung prediksi

Setelah model di-fit

  1. Nilai fit untuk $x$ asli

  2. Nilai $x$ baru untuk nilai prediksi

Diagram proses pemodelan dan perhitungan prediksi.

Generalized Linear Models di Python

Menghitung prediksi

  • Model kepiting tapal kuda y ~ weight $$ \mu = \frac{\exp(-3.6947+1.8151 \times weight)}{1+\exp(-3.6947+1.8151 \times weight)} $$

  • Pengukuran baru: 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 di Python

Prediksi di Python

  • Hitung prediksi model untuk dataset new_data
    # Compute model predictions
    model_GLM.predict(exog = new_data)
    
Generalized Linear Models di Python

Dari probabilitas ke kelas

Pemisahan keluaran model berdasarkan ambang probabilitas yang ditetapkan.

Generalized Linear Models di Python

Menghitung prediksi kelas

# 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 di Python

Menghitung prediksi kelas

# Count occurences for each class
crab['pred_class'].value_counts()
1    151
0     22
Ambang $\hat y=1$ $\hat y=0$
$\mu = 0.4$ 151 22
$\mu = 0.5$ 126 47
Generalized Linear Models di Python

Confusion matrix

Matriks kebingungan kosong.

Generalized Linear Models di Python

Confusion matrix - True Negative

Ilustrasi true negative pada confusion matrix.

Generalized Linear Models di Python

Confusion matrix - True Positive

Ilustrasi true negative dan true positive pada confusion matrix.

Generalized Linear Models di Python

Confusion matrix - False Positive

Ilustrasi true negative, true positive, dan false positive pada confusion matrix.

Generalized Linear Models di Python

Confusion matrix - False Negative

Ilustrasi true negative, true positive, false positive, dan false negative pada confusion matrix.

Generalized Linear Models di Python

Confusion matrix di 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 di Python

Ayo berlatih!

Generalized Linear Models di Python

Preparing Video For Download...