Sigmoid Function
How do you transform a model's raw, unbounded score into a meaningful probability between 0 and 1? The sigmoid function provides this transformation, mapping any real number to a value within the interval, making it perfect for binary classification tasks where outcomes are probabilistic.
The S-Curve Solution
Many machine learning models, especially linear ones, produce outputs that can range from negative infinity to positive infinity. For classification, we need a way to interpret these outputs as probabilities. The sigmoid function is specifically engineered to take any real number as input and squish it into a value that always falls between 0 and 1, exclusively.
The visualization clearly shows the sigmoid function's characteristic S-shape. Inputs around zero are mapped close to 0.5, indicating an equal chance of either outcome. As inputs become increasingly positive, the output smoothly approaches 1, never quite reaching it. Conversely, as inputs become increasingly negative, the output smoothly approaches 0, also without ever quite reaching it.
This asymptotic behavior is crucial for probabilities, as it ensures that the predicted values are always valid and interpretable. The smooth, continuous transition also means that small changes in the input score lead to small, predictable changes in the output probability, which is desirable for model stability.
The sigmoid function, often denoted by , is mathematically defined as:
Here, is Euler's number (the base of the natural logarithm), and is the input score from the model.
Implementing the Sigmoid Function
scores array in the code above to include very large positive and negative numbers, such as 100 and -100. Observe how the output probabilities behave at these extremes.Connecting Sigmoid to Logistic Regression
In logistic regression, the sigmoid function plays a central role. The model first calculates a linear combination of input features and their corresponding weights, often called the 'logit' or 'log-odds'. This linear output, , can be any real number.
The sigmoid function then takes this as its input, transforming it into a predicted probability for the positive class. This means , ensuring that the model's final output is a valid probability between 0 and 1, which can then be used for classification by setting a threshold (e.g., if , predict positive).
The sigmoid function maps any real number to a value strictly between 0 and 1.
Its characteristic S-shape and asymptotic behavior make it ideal for representing probabilities.
The mathematical definition is .
In logistic regression, the sigmoid function transforms a linear model's raw output (logit) into a predicted probability for the positive class.
The sigmoid function directly answers how to transform raw, unbounded scores into meaningful probabilities for binary classification.
Try it yourself
Logistic Regression Playground
Move a decision boundary and watch log-loss respond. See what the sigmoid turns distance into, and why a confident mistake costs so much.
Open the lab