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 (0,1)(0, 1) 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 Sigmoid Function: Mapping Scores to Probabilities
This chart illustrates how the sigmoid function transforms any real-valued input score into an output probability between 0 and 1, creating a characteristic S-shaped curve. As input scores become very negative, the output approaches 0; as they become very positive, the output approaches 1.
Loading chart...
Key Insight: The sigmoid function compresses all real numbers into the (0, 1) interval, making it ideal for probability estimation.

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.

📐 Sigmoid Function Formula

The sigmoid function, often denoted by σ(x)\sigma(x), is mathematically defined as:

σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}}

Here, ee is Euler's number (the base of the natural logarithm), and xx is the input score from the model.

Sigmoid Function
A mathematical function that maps any real number to a value between 0 and 1, exclusively. It is commonly used in logistic regression and neural networks to transform linear outputs into probabilities or activation values.
Example: If a logistic regression model outputs a raw score of x=2x = 2, applying the sigmoid function σ(2)≈0.88\sigma(2) \approx 0.88 transforms this score into an 88% probability of the positive class.

Implementing the Sigmoid Function

pythonCalculating Sigmoid Outputs in Python
Try It Yourself
Modify the 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.
python
Check Your Understanding
Which property of the sigmoid function makes it ideal for converting a model's raw score into a probability?

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, z=β0+β1x1+⋯+βnxnz = \beta_0 + \beta_1 x_1 + \dots + \beta_n x_n, can be any real number.

The sigmoid function then takes this zz as its input, transforming it into a predicted probability p^\hat{p} for the positive class. This means p^=σ(z)\hat{p} = \sigma(z), 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 p^>0.5\hat{p} > 0.5, predict positive).

Key Takeaways
  • 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 σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}}.

  • 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

← All lessons in Logistic Regression

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free