From Linear to Logistic: The Sigmoid and Log-Loss
Explore how logistic regression transforms linear predictions into probabilities using the sigmoid function and learns through log-loss to penalize incorrect predictions. Understand the importance of probabilistic outputs in classification tasks like fraud detection and loan default prediction, and how this approach improves model reliability over simple linear regression.
We'll cover the following...
A fraud screening model must return a risk score for a payment, not an unbounded estimate. For example, a payment worth $20 might receive a fraud probability near 0.05, while a $4,000 payment might receive a higher probability. The final decision can use that score, but the score itself must behave like a probability.
A linear regression model can learn a line from transaction amount to the binary fraud label 0 or 1. The wrong intuition is that a line constrained by examples labeled 0 and 1 will also stay between 0 and 1 for new payment amounts. A straight line has no such boundary. It can return a negative number for a small amount or a number greater than 1 for a large amount.
Lines 7–12: The small in-memory transaction table uses a binary
is_fraudlabel, but fittingLinearRegressionon it still learns an unconstrained line rather than a probability model.Lines 20–25: Predicting on extreme amounts (like
4000and5000) makes the unbounded nature of a linear model visible; theoutside_[0,1]flag highlights any raw predictions that fall below0or above1, which is why these outputs cannot be interpreted as probabilities.Lines 27–31: Display formatting is set so the printed table is compact and the numeric issue is easy to spot (e.g., values like
5.7420and7.1658stand out clearly).
This code produces raw predictions outside the valid probability range. A prediction such as 5.74 cannot mean a 574% chance of fraud, and a negative prediction, which a line can also produce for other inputs, could not be a probability either. For a binary outcome, where the label is either 0 for not fraud or 1 for fraud, the model needs an output strictly between 0 and 1.
Modeling requirement: A fraud risk score can act as a probability only when it falls between0and1.
Mapping scores to probabilities
Because a linear score can take any real value, logistic regression passes that score through the sigmoid function, a function that maps every real-valued input into the open interval from 0 to 1. The model can still use a linear combination of features internally, but the sigmoid converts the resulting score into a valid fraud probability.
For a score
When