Advanced Mathematics

Logistic Regression and Classification

A linear score passed through the sigmoid σ(z)=1/(1+e−z)\sigma(z) = 1/(1 + e^{-z}) to give a probability, trained by minimising cross-entropy.

Needs a step first: 32

To decide yes or no, compute a score z=w⃗⋅x⃗+bz = \vec w\cdot\vec x + b and squash it into a probability p^=σ(z)\hat p = \sigma(z) between 0 and 1. The boundary z=0z = 0 is a straight line (or flat plane) through the input space. The loss is the cross-entropy −[ylog⁡p^+(1−y)log⁡(1−p^)]-[y\log\hat p + (1 - y)\log(1 - \hat p)], which is the negative log-likelihood, and its gradient comes out neatly as (p^−y)x⃗(\hat p - y)\vec x. A logistic regression unit is one neuron; stacking many gives a neural network, and softmax extends it to many classes.

Where it shows up: Each unit of a neural network is a logistic regression with a different activation, and softmax is its many-class form.

How to hold it: In your head

See a linear score squashed by the sigmoid into a probability, the boundary z=0z=0 as a straight line through the inputs, and the gradient (p^−y)x⃗(\hat p-y)\vec x as error times input.

What knowing it looks like

  • Compute a logistic unit's probability, loss and gradient for one example by hand.
  • Derive σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)) and the gradient (p^−y)x⃗(\hat p - y)\vec x.
  • Draw the decision boundary of a two-input logistic model from its weights.

Test yourself

A logistic model has w⃗=(2,−1)\vec w = (2, -1) and b=0.5b = 0.5. For input x⃗=(1,2)\vec x = (1, 2) with label y=1y = 1, find zz, p^\hat p, the cross-entropy loss in nats, and the gradient of the loss with respect to w⃗\vec w and bb. Use e−0.5≈0.6065e^{-0.5} \approx 0.6065.
Show the worked answer
z=2⋅1−1⋅2+0.5=0.5z = 2\cdot1 - 1\cdot2 + 0.5 = 0.5. p^=1/(1+0.6065)≈0.622\hat p = 1/(1 + 0.6065) \approx 0.622. Loss =−ln⁡0.622≈0.474= -\ln 0.622 \approx 0.474. With p^−y≈−0.378\hat p - y \approx -0.378: ∇w⃗=(p^−y)x⃗≈(−0.378,−0.755)\nabla_{\vec w} = (\hat p - y)\vec x \approx (-0.378, -0.755) and ∂/∂b≈−0.378\partial/\partial b \approx -0.378. A gradient step therefore raises w1w_1, w2w_2 and bb, pushing p^\hat p towards 1.

Needs a step first: 32

Learn it with an AI

Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.

Be my maths tutor. I'm learning "Logistic Regression and Classification" (Undergraduate II tier): A linear score passed through the sigmoid $\sigma(z) = 1/(1 + e^{-z})$ to give a probability, trained by minimising cross-entropy.
I already know: Linear Regression as Learning, Maximum Likelihood.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: A logistic model has $\vec w = (2, -1)$ and $b = 0.5$. For input $\vec x = (1, 2)$ with label $y = 1$, find $z$, $\hat p$, the cross-entropy loss in nats, and the gradient of the loss with respect to $\vec w$ and $b$. Use $e^{-0.5} \approx 0.6065$.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.

On the way to

Recommended