Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Logistische regressie met gradient descent

In deze lab-sessie passen we logistische regressie met gradient descent toe op de Iris dataset. We bouwen een binaire classifier die onderscheid maakt tussen twee soorten irissen op basis van hun bloemeigenschappen.

Leerdoelenยถ

  • Logistische regressie implementeren met gradient descent

  • Binary cross-entropy loss berekenen en minimaliseren

Extrasยถ

  • Model performance evalueren

  • Decision boundaries visualiseren

1. Data Loading en Exploratieยถ

De Iris dataset bevat metingen van 150 iris bloemen van 3 verschillende soorten:

  • Setosa

  • Versicolor

  • Virginica

Voor elk bloem zijn er 4 features gemeten (in cm):

  • Sepal length (kelkblad lengte)

  • Sepal width (kelkblad breedte)

  • Petal length (bloemblad lengte)

  • Petal width (bloemblad breedte)

โš ๏ธ Voor deze oefening maken we een binaire classificatie: we onderscheiden alleen tussen Versicolor (klasse 0) en Virginica (klasse 1).

Total samples: 100
Versicolor (0): 50
Virginica (1): 50

Features: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

First few rows:
Loading...
Loading...
Loading...

2. Data Voorbereidingยถ

We splitsen de data in:

  • Training set (80%): gebruikt om het model te trainen

  • Test set (20%): gebruikt om het model te evalueren

We gebruiken feature scaling (standaardisatie) om ervoor te zorgen dat alle features dezelfde schaal hebben. Dit helpt gradient descent sneller convergeren.

Training set: 80 samples
Test set: 20 samples
Number of features (including intercept): 5

Class distribution in training set:
  Versicolor (0): 40 (50.0%)
  Virginica (1): 40 (50.0%)

3. Logistische Regressie Implementatieยถ

We implementeren logistische regressie met gradient descent zoals gezien in de cursus:

Modelยถ

y^=ฯƒ(Xb)=11+eโˆ’Xb\pmb{\hat{y}} = \sigma(\pmb{X}\pmb{b}) = \frac{1}{1 + e^{-\pmb{X}\pmb{b}}}

Binary Cross-Entropy Lossยถ

LBCE(b)=โˆ’1nโˆ‘i=1n[yilogโก(y^i)+(1โˆ’yi)logโก(1โˆ’y^i)]\mathcal{L}_{BCE}(\pmb{b}) = -\frac{1}{n} \sum_{i=1}^{n} \left[ y_i \log(\hat{y}_i) + (1 - y_i) \log(1 - \hat{y}_i) \right]

Gradiรซntยถ

โˆ‡LBCE=1nXT(y^โˆ’y)\nabla \mathcal{L}_{BCE} = \frac{1}{n} \pmb{X}^T(\pmb{\hat{y}} - \pmb{y})

Gradient Descent Updateยถ

bk+1=bkโˆ’ฮปโˆ‡LBCE(bk)\pmb{b}^{k+1} = \pmb{b}^k - \lambda \nabla \mathcal{L}_{BCE}(\pmb{b}^k)
Functions defined:
  - sigmoid(z): Logistic activation function
  - bce_loss(X, y, b): Binary cross-entropy loss
  - gradient_bce(X, y, b): Gradient of BCE loss
  - accuracy(X, y, b): Classification accuracy

4. Training met Gradient Descentยถ

Nu trainen we het model met gradient descent. We houden de loss en accuracy bij voor zowel de training als test set.

Starting gradient descent...
Learning rate: 0.1
Max iterations: 5000

Initial values:
  Train Loss: 0.6931
  Test Loss: 0.6931
  Train Accuracy: 50.00%
  Test Accuracy: 50.00%

Training...

โœ“ Training completed!

Final results:
  Train Loss: 0.0555
  Test Loss: 0.1357
  Train Accuracy: 96.25%
  Test Accuracy: 95.00%

Final parameters (b):
  Bias (bโ‚€): 0.4535
  sepal length (cm): -0.5804
  sepal width (cm): -0.9527
  petal length (cm): 3.9708
  petal width (cm): 5.3047

โœ“ Training completed!

Final results:
  Train Loss: 0.0555
  Test Loss: 0.1357
  Train Accuracy: 96.25%
  Test Accuracy: 95.00%

Final parameters (b):
  Bias (bโ‚€): 0.4535
  sepal length (cm): -0.5804
  sepal width (cm): -0.9527
  petal length (cm): 3.9708
  petal width (cm): 5.3047

5. Visualisatie van Training Procesยถ

We visualiseren hoe de loss en accuracy evolueren tijdens het training proces.

Loading...
Loading...

6. Model Evaluatie en Predictionsยถ

Laten we kijken naar de voorspellingen van ons model en hoe goed het presteert.

Test Set Predictions:
    True Label True Species  Predicted Prob  Predicted Label  \
0            1    Virginica        0.999416                1   
1            1    Virginica        0.801364                1   
2            1    Virginica        0.997942                1   
3            1    Virginica        0.991538                1   
4            0   Versicolor        0.000002                0   
5            0   Versicolor        0.000036                0   
6            0   Versicolor        0.617276                1   
7            1    Virginica        0.994276                1   
8            0   Versicolor        0.137936                0   
9            0   Versicolor        0.291849                0   
10           0   Versicolor        0.000158                0   
11           1    Virginica        0.998850                1   
12           0   Versicolor        0.000512                0   
13           0   Versicolor        0.000304                0   
14           0   Versicolor        0.000016                0   
15           1    Virginica        0.999984                1   
16           1    Virginica        0.687062                1   
17           0   Versicolor        0.022189                0   
18           1    Virginica        0.699027                1   
19           1    Virginica        0.767607                1   

   Predicted Species  Correct  
0          Virginica     True  
1          Virginica     True  
2          Virginica     True  
3          Virginica     True  
4         Versicolor     True  
5         Versicolor     True  
6          Virginica    False  
7          Virginica     True  
8         Versicolor     True  
9         Versicolor     True  
10        Versicolor     True  
11         Virginica     True  
12        Versicolor     True  
13        Versicolor     True  
14        Versicolor     True  
15         Virginica     True  
16         Virginica     True  
17        Versicolor     True  
18         Virginica     True  
19         Virginica     True  

Test Accuracy: 95.00%
Correct predictions: 19 / 20
Loading...

Confusion Matrix Analysis:
True Negatives (Versicolor correctly classified): 9
False Positives (Versicolor misclassified as Virginica): 1
False Negatives (Virginica misclassified as Versicolor): 0
True Positives (Virginica correctly classified): 10

7. Decision Boundary Visualisatieยถ

We visualiseren de decision boundary van ons model voor twee features tegelijk. We gebruiken petal length en petal width omdat deze features het best discrimineren tussen de twee soorten.

2D Model Test Accuracy: 85.00%
2D Model Parameters: bโ‚€=0.536, bโ‚=4.011, bโ‚‚=5.436
Loading...

Decision boundary equation (in standardized space):
  0.536 + 4.011ร—(petal_length) + 5.436ร—(petal_width) = 0

8. Interactieve Visualisatie met Plotlyยถ

Laten we een interactieve 3D visualisatie maken om te zien hoe de voorspelde waarschijnlijkheid varieert in de feature space.

Loading...