Skip to main content

ML01.6 · Logistic regression and classification

A free, illustrated module of 4 lessons and one interactive in ML01 Machine Learning Foundations: From a line to a probability; The log loss; Training by gradient descent; Decision boundaries and thresholds.

ML01.6 L1 · Core 4 lessons · about 100 min Live · in review

Part of ML01 Machine Learning Foundations in the Machine Learning track.

Many questions have a yes-or-no answer: will this student pass, is this email spam, is this tumour malignant? A straight line is the wrong tool for them, and logistic regression is the right first one. These four lessons bend the line into an S-shaped curve that outputs a probability, derive the log loss that scores such a curve from the likelihood of the data, train it by gradient descent with a gradient that turns out to be prediction minus label, and then turn its probabilities into decisions with a boundary and a threshold.

What you need first. A line as a model and the mean squared error (ML01.4), gradient descent and the learning rate (ML01.5), exponentials and logarithms (M01.4), and the chain rule (M04.3). The idea that independent probabilities multiply (M06.3) is used once, in lesson 2.

Lessons

  1. From a line to a probability ML01.6.1 25 min

    A straight line is a poor model for a yes-or-no label: it predicts numbers below 0 and above 1. The sigmoid squashes any score into a probability, and the model that results is still a straight line, in the log-odds.

  2. The log loss ML01.6.2 25 min

    Score a probability curve by how probable it makes the labels that were actually seen. Taking logarithms turns that product into a sum, the mean negative log-likelihood or cross-entropy, and shows why squared error is the wrong loss for a sigmoid.

  3. Training by gradient descent ML01.6.3 25 min

    Differentiate the sigmoid, then the log loss, and the gradient collapses to prediction minus label, times the feature. Train logistic regression from scratch, match scikit-learn’s LogisticRegression, and see what its default regularisation changes.

  4. Decision boundaries and thresholds ML01.6.4 25 min

    A probability becomes a decision when it is compared with a threshold. The border between the two answers is where the score is zero: a straight line in two features. Moving the threshold trades false alarms for misses, and softmax extends the model to many classes.

The interactive

ML01.6-X1 · Sigmoid and boundary explorer. Set the weight and the bias of a logistic regression by hand and watch the probability curve, the log loss of every student and the mistakes at your chosen threshold. Then press Train and let gradient descent find the best curve, for one feature or for two. It lives in Training by gradient descent.

Back to top