Logistic Regression › Formula
Machine Learning from ScratchAll chapters Logistic Regression › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The core formula behind logistic regression involves transforming a linear combination of features into a probability using the logistic function. Let's break this down step by step.
The Linear Predictor
First, we start with a linear combination of features, just like in linear regression:
where:
- is the linear predictor (a scalar value for a single observation that can take any real value from to )
- is the intercept (the baseline log-odds when all features equal zero)
- are the regression coefficients for each feature (quantify the effect of each feature on the log-odds)
- are the feature values (predictor variables for a single observation)
- is the number of features (total count of predictor variables in the model)
This linear predictor can take any real value from to .
The Logistic Function
The main idea of logistic regression is the logistic function (also called the sigmoid function), which maps the linear predictor into a probability:
where:
- is the predicted probability (bounded between 0 and 1, representing the probability that )
- is Euler's number (approximately 2.718, the base of the natural logarithm)
- is the linear predictor from above (the linear combination )
Let's understand why this transformation works:
When is very large (positive):
- becomes very small (close to 0)
- becomes close to 1
When is very small (negative):
- becomes very large
- becomes very large
When :
Let's visualize the logistic function to better understand its behavior:

This visualization shows the characteristic S-shaped curve of the logistic function. Notice how:
At η = 0: The probability is exactly 0.5, and the curve is steepest here
As η becomes very negative: The probability approaches 0 asymptotically
As η becomes very positive: The probability approaches 1 asymptotically
The curve is smooth: No sharp corners or discontinuities, making optimization tractable
The curve is symmetric: Around the point (0, 0.5)
The Logit Function (Inverse of Logistic)
The logit function is the inverse of the logistic function and represents the log-odds:
where:
- is the logit function (transforms probabilities to log-odds)
- is the probability (bounded between 0 and 1)
- is the odds (the ratio of the probability of success to the probability of failure)
- is the natural logarithm (base )
- is the linear predictor (the right-hand side: )
This equation reads as: "The log-odds of the probability equals the linear predictor."
Let's visualize this relationship to understand why logistic regression models the log-odds as a linear function:

This visualization shows why logistic regression models the log-odds rather than the probability directly. The logit transformation converts probabilities (which are bounded between 0 and 1) into log-odds (which can be any real number from to ). This unbounded range makes it possible to use a linear model:
At p = 0.5: The log-odds are 0, representing equal odds for both outcomes
As p approaches 0: The log-odds approach , representing very low odds
As p approaches 1: The log-odds approach , representing very high odds
The relationship is smooth and monotonic: Higher probabilities correspond to higher log-odds
Complete Logistic Regression Model
Putting it all together, the complete logistic regression model is:
where:
- is the probability that the response variable equals 1 given predictors (the parameter we're modeling)
- is the odds of success (the ratio of the probability of success to the probability of failure)
- is the log-odds or logit (the natural logarithm of the odds)
- is the intercept (the log-odds when all features equal zero)
- are the regression coefficients (represent the change in log-odds for a one-unit increase in each feature)
- are the feature values (predictor variables for a single observation)
- is the number of features (total count of predictor variables in the model)
This is read as: "The log-odds of the probability equals the linear combination of features."
Matrix Notation
In matrix form, logistic regression can be written as:
where:
- is the vector of predicted probabilities (an vector where each element is the predicted probability for one observation)
- is the design matrix of features (an matrix including a column of 1s for the intercept)
- is the vector of coefficients (a vector containing the intercept and all feature coefficients)
- is the number of observations (rows in the dataset)
- is the number of features (excluding the intercept)
- The logit function is applied element-wise to
Mathematical Properties
Bounded Output: The logistic function ensures probabilities remain between 0 and 1
Smooth and Continuous: The function is differentiable everywhere, making optimization tractable
Symmetric: The function is symmetric around 0.5 when (i.e., )
Monotonic: As the linear predictor increases, the probability increases monotonically
Asymptotic: The function approaches 0 and 1 asymptotically without reaching them
Convex Log-likelihood: The negative log-likelihood is convex, so a unique global maximum