2.2.9Linear & Logistic Regression

Logistic regression and the sigmoid function

1,915 words9 min readdifficulty · medium2 backlinks

WHAT is logistic regression?

Figure — Logistic regression and the sigmoid function

WHY the sigmoid, specifically? (Derivation from odds)

We want a function that turns a linear score into a probability. Start from odds, not probability.


HOW do we train it? (The loss and its gradient)

We can't use squared error nicely (it's non-convex for sigmoid). Instead use maximum likelihood.


The decision boundary


Worked examples


Common mistakes


Flashcards

What does the sigmoid function map its input to?
The open interval (0,1)(0,1), interpreted as a probability.
Write the sigmoid formula.
σ(z)=11+ez\sigma(z)=\dfrac{1}{1+e^{-z}}.
What is σ(0)\sigma(0)?
0.50.5.
What is the derivative σ(z)\sigma'(z)?
σ(z)(1σ(z))\sigma(z)(1-\sigma(z)).
What quantity does logistic regression assume is linear in xx?
The log-odds (logit), lnp1p=wx+b\ln\frac{p}{1-p}=w^\top x+b.
Why not use squared error for logistic regression?
It becomes non-convex and its gradient saturates when very wrong; cross-entropy is convex.
State the binary cross-entropy loss for one example.
[ylnp+(1y)ln(1p)]-[y\ln p + (1-y)\ln(1-p)] with p=σ(z)p=\sigma(z).
What is the gradient J/wj\partial J/\partial w_j?
1mi(piyi)xij\frac{1}{m}\sum_i (p_i-y_i)x_{ij} — (pred−target)×input.
Where is the decision boundary?
Where wx+b=0w^\top x + b = 0 (equivalently p=0.5p=0.5); it is linear.
If log-odds =1.1=1.1, what are the odds and pp?
Odds =e1.13=e^{1.1}\approx3, so p=0.75p=0.75.
Is logistic regression classification or regression?
Classification (name is historical).

Recall Feynman: explain to a 12-year-old

Imagine a machine that gives a "yes-ness score" — big positive means "definitely yes", big negative means "definitely no", zero means "no idea". But a score like 7 or −3 is confusing. So we pass it through a special squishy slide (the sigmoid) that turns any score into a number between 0 and 1 — like a percentage of confidence. Score of 0 → 50%. Big score → near 100%. We then teach the machine by nudging its dials whenever it's too confident and wrong, using the simple rule "how wrong you were × the input".

Connections

Concept Map

inadequate for yes/no

squashed by

outputs

take log

set equal to

solve for p, invert

defines

linear

trained by

negative log yields

log-likelihood

motivates

Linear regression predicts number

Need probability in 0 to 1

Linear score z = wx + b

Sigmoid function

P y=1 given x

Odds p over 1-p

Log-odds logit

Logistic regression classifier

Linear decision boundary

Maximum likelihood

Cross-entropy loss

Bernoulli label model

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, linear regression toh ek number predict karta hai jo kuch bhi ho sakta hai — minus infinity se plus infinity tak. Par jab sawaal "haan ya na" ka ho (spam hai ya nahi, tumor malignant hai ya nahi), tab humein ek probability chahiye jo 0 aur 1 ke beech ho. Iske liye hum pehle ek linear score nikalte hain z=wx+bz = w^\top x + b, aur phir usko sigmoid function se dabaa dete hain: σ(z)=1/(1+ez)\sigma(z)=1/(1+e^{-z}). Ye har score ko 0 aur 1 ke beech le aata hai, jise hum P(y=1)P(y=1) maante hain.

Sigmoid koi random function nahi hai. Agar tum log-odds (lnp1p\ln\frac{p}{1-p}) ko linear maan lo, toh algebra se solve karne par automatically sigmoid nikal aata hai. Isliye ye natural choice hai. Boundary wahan hoti hai jahan z=0z=0, matlab p=0.5p=0.5 — aur ye boundary hamesha ek seedhi line/hyperplane hoti hai, isiliye ise "linear" model kehte hain.

Training ke liye squared error mat use karo — wo yahan non-convex ho jaata hai aur galat jagah gradient zero ho jaata hai. Iski jagah cross-entropy (log loss) use karte hain, jo maximum likelihood se aata hai aur convex hota hai. Sabse pyari baat: gradient ban jaata hai bilkul simple — (py)x(p - y)\cdot x, yaani (prediction minus asli answer) guna input. Bilkul linear regression jaisa, bas prediction ki jagah sigmaid output.

Yaad rakhne ka trick: "Log-odds go linear, then squash." Aur sigmoid ka derivative? "s times one-minus-s" — σ(1σ)\sigma(1-\sigma). Bas itna pakka kar lo toh logistic regression tumhaara dost ban jaayega.

Go deeper — visual, from zero

Test yourself — Linear & Logistic Regression

Connections