Skip to content

Commit 4bbf959

Browse files
committed
code example and the confusion matrix plot for logistic regression in episode 4
1 parent 4c95d02 commit 4bbf959

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

content/04-supervised-ML-classification.rst

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ Despite its name, logistic regression is not a regression algorithm but a classi
319319
For binary classification, it uses the logistic (**sigmoid**) function to map a linear combination of input features to a probability between 0 and 1, which is then thresholded (typically at 0.5) to assign a class.
320320

321321
For a multiclass classification, logistic regression can be extended using strategies like **one-vs-rest** (OvR) or softmax regression.
322+
322323
- in OvR, a separate binary classifier is trained for each species against all others.
323324
- **softmax regression** generalizes the logistic function to compute probabilities across all classes simultaneously, selecting the class with the highest probability.
324325

@@ -328,25 +329,29 @@ For a multiclass classification, logistic regression can be extended using strat
328329

329330
1) The sigmoid function; 2) the softmax regression process: three input features to the softmax regression model resulting in three output vectors where each contains the predicted probabilities for three possible classes; 3) a bar chart of softmax outputs in which each group of bars represents the predicted probability distribution over three classes; 4-6) a binary classifier distinguishes one class from the other two classes using the one-vs-rest approach.
330331

332+
The creation of a Logistic Regression model and the process of fitting it to the training data are nearly identical to those used for the KNN model described above, except that a different classifier is selected. The code example and the resulting confusion matrix plot are provided below:
333+
334+
.. code-block:: python
331335
332-
```
333-
from sklearn.linear_model import LogisticRegression
336+
from sklearn.linear_model import LogisticRegression
334337
335-
lr_clf = LogisticRegression(random_state = 0)
336-
lr_clf.fit(X_train_scaled, y_train)
338+
lr_clf = LogisticRegression(random_state = 0)
339+
lr_clf.fit(X_train_scaled, y_train)
337340
338-
y_pred_lr = lr_clf.predict(X_test_scaled)
341+
y_pred_lr = lr_clf.predict(X_test_scaled)
339342
340-
score_lr = accuracy_score(y_test, y_pred_lr)
341-
print("Accuracy for Logistic Regression:", score_lr )
342-
print("\nClassification Report:\n", classification_report(y_test, y_pred_lr))
343+
score_lr = accuracy_score(y_test, y_pred_lr)
344+
print("Accuracy for Logistic Regression:", score_lr )
345+
print("\nClassification Report:\n", classification_report(y_test, y_pred_lr))
343346
344-
# compute and plot confusion matrix
345-
cm_lr = confusion_matrix(y_test, y_pred_lr)
346-
plot_confusion_matrix(cm_lr, "Confusion Matrix using Logistic Regression algorithm", "confusion-matrix-lr.png")
347-
```
347+
# compute and plot confusion matrix
348+
cm_lr = confusion_matrix(y_test, y_pred_lr)
349+
plot_confusion_matrix(cm_lr, "Confusion Matrix using Logistic Regression algorithm", "confusion-matrix-lr.png")
348350
349351
352+
.. figure:: img/confusion-matrix-lr.png
353+
:align: center
354+
:width: 384px
350355

351356

352357

content/img/confusion-matrix-lr.png

25 KB
Loading

0 commit comments

Comments
 (0)