Logistic regression is the fundamental algorithm for binary classification problems in aerospace ML. Instead of predicting continuous values (like altitude), we predict probabilities (like "will this component fail?" or "is this satellite image cloudy?"). It maps any real-valued input to a probability between 0 and 1 using the sigmoid function, then trains by minimizing cross-entropy loss.
The sigmoid function does exactly this. Think of it as a "probability translator" — it takes your model's raw confidence score and converts it to a valid probability.
Why this specific form? Start with the odds ratio. If probability of class 1 is p, the odds are:
odds=1−pp
In logistic regression, we assume the log-odds are linear in features:
log(1−pp)=z=wTx+b
Why? This is the key assumption — the effect of features is multiplicative on odds, but additive on log-odds. Now solve for p:
1−pp=ezp=ez(1−p)p=ez−pezp(1+ez)=ezp=1+ezez
Multiply numerator and denominator by e−z:
p=e−z+11=σ(z)
Key Properties:
σ(0)=0.5 (decision boundary)
σ(z)→1 as z→∞
σ(z)→0 as z→−∞
Derivative: σ′(z)=σ(z)(1−σ(z)) (convenient for backprop!)
Derivative Derivation:dzdσ(z)=dzd(1+e−z1)
Using chain rule with u=1+e−z:
=−u21⋅(−e−z)=(1+e−z)2e−z=1+e−z1⋅1+e−ze−z=σ(z)⋅1+e−ze−z=σ(z)⋅(1−1+e−z1)=σ(z)(1−σ(z))
Why this step? The derivative form makes gradient calculations elegant — no exponentials in the gradient updates!
Why this step? We compute the linear combination first, then pass through sigmoid to get probability. Note we deliberately use w=0.0001 throughout so that z does not saturate the sigmoid.
Loss for this example:L(2)=−[1⋅log(0.5498)+0⋅log(0.4502)]=−log(0.5498)=0.598
Why negative? Prediction is too low (0.55 vs 1), so we need to increasew to increase prediction. Negative gradient means descent will add to w.
Update (α=10−6):
Because the raw feature (x=5000) is large, the gradient is large, so we pick a tiny learning rate to avoid overshooting:
w:=0.0001−10−6⋅(−2251)=0.0001+0.002251=0.002351
Why this step? With unnormalized features the gradient magnitude scales with x, so α must be small. (In practice we normalize features — see Mistake 2 — which lets us use a larger, more stable α.) After the update w increases, making the model predict a higher probability for high operating hours.
Problem: Binary classifier for cloud presence. Features: average pixel brightness (x1), brightness variance (x2).
Training point: x=[180,45], y=1 (cloudy)
Parameters: w=[0.01,−0.02], b=−1.5
Why this step? Both gradients are negative because prediction is too low. Updates will increase both weights to make the model more "pro-cloudy" for these feature values.
Wrong Thinking: "Sigmoid gives 0.7, so output is 0.7?"
Why It Feels Right: Sigmoid outputs a number, so use it directly.
The Fix:y^ is a probability, not the final class. Apply threshold:
prediction_class = 1 if y_hat >= 0.5 else 0
For aerospace safety applications, you might use threshold 0.3 (prefer false alarms over missed failures).
Why This Works: Probabilities inform decisions based on cost/benefit. A 0.7 probability doesn't mean "70% of the answer is class 1" — it means "70% confidence in class 1."
Wrong Code:
# x1 in [0, 1000] hours, x2 in [0, 1] booleanz = w1 * x1 + w2 * x2 + b
Why It Fails:w1 will dominate because x1 scale is huge. Gradient updates become unstable and force you to use a tiny learning rate (as we saw in Example 1).
The Fix: Standardize features:
xjnorm=σjxj−μj
from sklearn.preprocessing import StandardScalerscaler = StandardScaler()X_scaled = scaler.fit_transform(X)
Why This Works: All features contribute equally. Gradients have similar magnitudes, making optimization converge faster and letting you use a larger, stable learning rate.
Wrong Math: Computing L=−[ylogy] instead of L=−[ylogy^]
Why It Happens: Confusing true label y with predicted probability y^.
The Fix: Cross-entropy measures divergence between:
True distribution: y (either 0 or 1)
Predicted distribution: y^ (probability from sigmoid)
Cross-Entropy: "Punish Confident Wrongs"−log grows huge as probability → 0. If model is confident (high y^) but wrong (y=0), loss explodes.
Recall
Feynman Explanation (Explain to a 12-Year-Old)
Imagine you're guessing if a coin is heads or tails based on how shiny it is. You can't just say "yes" or "no" — you need to say HOW SURE you are, like "70% sure it's heads."
The sigmoid function is like a special ruler that turns any measurement (shine level) into a percentage between 0% and 100%. If shine is really high, sigmoid says "almost 100% heads." If shine is really low, sigmoid says "almost 0% heads" (so tails!). If shine is medium, sigmoid says "50% — could be either."
Now, how do we learn what "high" and "low" shine mean? We show the computer lots of coins with their true answers. Cross-entropy is the "wrongness score" — if you said "90% heads" but it was tails, you get a HUGE penalty. If you said "51% heads" and it was heads, tiny penalty. The computer adjusts its ruler to make the wrongness score as small as possible.
In aerospace, instead of coins, we might be guessing "will this engine part break?" based on temperature and vibration. Same math!
What is the sigmoid function and why is it used in logistic regression?
σ(z)=1+e−z1. It maps any real-valued linear combination to a probability in (0,1), is differentiable everywhere, and its derivative has a simple form σ(z)(1−σ(z)) that makes gradient calculations efficient.
Derive the sigmoid function from the log-odds assumption
Assume log(1−pp)=z. Then 1−pp=ez, so p=ez(1−p), giving p(1+ez)=ez, thus p=1+ezez=1+e−z1.
What is binary cross-entropy loss for a single example?
L(y,y^)=−[ylog(y^)+(1−y)log(1−y^)] where y∈{0,1} is true label and y^∈(0,1) is predicted probability.
Why is cross-entropy loss used instead of MSE for logistic regression?
MSE is non-convex with sigmoid (multiple local minima) and has vanishing gradients when predictions saturate. Cross-entropy is convex, derived from maximum likelihood, and maintains strong gradients even for confident wrong predictions.
Derive the gradient of cross-entropy loss w.r.t. weight wj
Using chain rule: ∂wj∂L=∂y^∂L⋅∂z∂y^⋅∂wj∂z=[−y^y+1−y^1−y]⋅y^(1−y^)⋅xj=(y^−y)xj.
What is the decision boundary in logistic regression?
The hyperplane where wTx+b=0, corresponding to y^=σ(0)=0.5. Points one side are classified as 1, the other as 0.
What is the derivative of the sigmoid function?
σ′(z)=σ(z)(1−σ(z)). This is derived using the quotient/chain rule and makes backpropagation efficient since it only requires the sigmoid output, not recomputing the exponential.
Why must features be normalized in logistic regression?
Features with vastly different scales cause some weights to dominate and force tiny learning rates. Standardization (zero mean, unit variance) ensures all features contribute equally and gradients have similar magnitudes, accelerating convergence.
How is logistic regression related to maximum likelihood estimation?
The cross-entropy loss is the negative log-likelihood of a Bernoulli distribution. Minimizing loss = maximizing likelihood of observed labels given model parameters.
What threshold should be used for aerospace safety-critical classification?
Not always 0.5. For failure prediction, use lower threshold (e.g., 0.3) to prefer false alarms over missed failures. Threshold depends on cost asymmetry: cost(miss)≫cost(false alarm).
Dekho, yaha basic idea ye hai ki jab humein classification karna hota hai — jaise "ye component fail hoga ya nahi", ya "satellite image me cloud hai ya nahi" — tab humein continuous number nahi, balki ek probability chahiye jo 0 aur 1 ke beech ho. Ab problem ye hai ki normal linear regression (y=wx+b) toh koi bhi value de sakta hai, jaise -10 ya 5, jo probability ke liye galat hai. Isiliye hum sigmoid function use karte hai, jo kisi bhi real number ko squash karke (0,1) ke beech le aata hai. Simple bhaasha me sigmoid ek "probability translator" hai — model ka raw confidence score leta hai aur usko valid probability me convert kar deta hai.
Ab sigmoid kaha se aaya? Iska core intuition hai odds ratio. Hum maante hai ki log-odds features ke saath linear relation rakhte hai, matlab features ka effect odds pe multiplicative hota hai lekin log-odds pe additive. Jab isko solve karte hai toh automatically sigmoid formula nikal aata hai — ye koi random function nahi hai, ye ek natural derivation hai. Aur ek bada plus point ye hai ki sigmoid ka derivative bahut clean hai: σ′(z)=σ(z)(1−σ(z)), jisse gradient descent ke calculations easy ho jaate hai, koi extra exponentials ka jhanjhat nahi.
Loss function ke liye hum MSE nahi, balki cross-entropy loss use karte hai, aur iska reason important hai. MSE sigmoid ke saath non-convex ho jaata hai (matlab multiple local minima, training atak sakti hai) aur jab prediction galat hoti hai tab bhi gradient kamzor ho jaata hai. Cross-entropy convex hoti hai (ek hi global minimum), strong gradients deti hai, aur confident galat predictions ko heavily penalize karti hai — yani model "over-confident galti" karne pe zyada seekhta hai. Ye poori cheez maximum likelihood se derive hoti hai, jo mathematically prove karta hai ki ye loss sahi choice hai. Yahi combination — sigmoid plus cross-entropy — logistic regression ko aerospace ML me binary classification ka reliable foundation banata hai.