Logistic Regression
Suppose our answer needs to be yes or no. We can first estimate a probability from the measurements, then decide how large that probability needs to be before we choose yes.
18 sections · 10 questions · 4 coding challenges
The arrow keys move between sections too.
How Do We Predict an Outcome That Is Either Yes or No?
BOTIE
Logistic regression developed within the study of binary outcomes. David Cox's 1958 paper on binary sequences is an early treatment of regression for this kind of data. The useful change is in what we model with a linear relationship: the logarithm of the odds, rather than the probability itself.
We can think of the model as first collecting evidence into a score. The logistic function then turns that score into a probability between zero and one. Large positive scores approach one; large negative scores approach zero. This gives us a way to represent increasing confidence without leaving the range a probability is allowed to occupy.
Sources for this history
A Prediction Between Yes and No
Suppose a model must choose between two supplied categories. Measurements offer evidence, but the groups may overlap. We would like the model to express uncertainty before turning its estimate into a decision.
There are three jobs to separate: collect evidence, express a probability estimate, and apply a decision threshold.
Measurements provide evidence
Height and weight are inputs in the teaching example; the supplied category is the training answer. Measurements can point toward a category without guaranteeing it. An immediate yes-or-no answer hides that uncertainty.
Inputs
- Height
- Weight
Question
- Which of the two supplied categories?
The probability model and decision rule have different jobs
We can now name the linear score, sigmoid function, and threshold. Fitting adjusts the score's coefficients using labeled examples and a probability-based loss.
The full model shows the resulting estimates and boundary. Those estimates still need evaluation and calibration checks before we trust them as accurate frequencies.
Explore the full example
Read the score, probability, and predicted class as three different quantities. Move the query point and watch where the class changes.
…
Part 1. From Numeric Outcomes to Categories
1. A binary prediction problem
Each dot in the box above is one student. The input x is the number of hours they studied, and the outcome y is whether they passed. Fails sit on the floor of the plot and passes on the ceiling, because the outcome is one or the other with nothing between.
y = 1 for passed y = 0 for failedZero and one are labels, and nothing more. Passing is not one unit more than failing, and the model will not treat it so. The twelve students the page works with have a mixed middle. Below three and a half hours everyone failed, above five and a half everyone passed, and between them the two outcomes are tangled together. Someone at four and a half hours might go either way, and the model is going to use the hours to estimate how likely each way is.
2. Why a straight-line prediction is not a probability
The obvious first attempt is the regression page’s line, fitted to the zeros and ones as if they were numbers.
Sigmoid maps the score into the probability interval. That makes the output eligible for a probability interpretation, but does not by itself establish good calibration.
A straight line can produce any value. A probability has to stay between zero and one. This line predicts −0.09 for a student who studied one hour and 1.18 for one who studied eight, and neither is a chance of anything. The red stretches are every input at which the line has stopped meaning what it was asked to mean. A probability model has to return a value between zero and one for every possible input, not only for the inputs it happened to see.
Part 2. Score, Probability, and Decision
3. Score, probability, and decision
Three quantities run through this page, and keeping them separate is most of understanding the model.
| Quantity | What it is | Range |
|---|---|---|
| score z | the model's unrestricted numerical evidence for passing | any number |
| probability p | the estimated chance of passing | between 0 and 1 |
| predicted class ŷ | a pass or a fail, decided by comparing p to a threshold | 0 or 1 |
They are related, and each is computed from the one before, and they are not the same thing. The score is what the straight line produces. The probability is what the score becomes. The decision is a rule laid on top of the probability, and it is the only one of the three that has a threshold in it.
4. The linear score
Start with the calculation every linear model starts with.
z = β·x + αThe score can be any real number. Positive scores favour passing, negative scores favour failing, and zero is neutral. The score itself is not a probability. The fitted slope is about 1.69 and the intercept about −7.18.
score after four study hours = 1.69 × 4 − 7.18 = −0.42
score after five study hours = 1.69 × 5 − 7.18 = 1.27These calculations use rounded coefficients. The full fitted values give approximately −0.40 and 1.29. The sigmoid will turn each score into a probability.
5. Converting scores with the sigmoid
Whatever turns a score into a probability should send large negative scores near zero, send large positive scores near one, send zero to exactly one half, and do all of that smoothly. The sigmoid does.
p = σ(z) = 1 / (1 + e⁻ᶻ)Worked example
| z | e⁻ᶻ | p = 1 / (1 + e⁻ᶻ) |
|---|---|---|
| −4 | 54.6 | 0.018 |
| −2 | 7.39 | 0.119 |
| 0 | 1 | 0.500 |
| 2 | 0.135 | 0.881 |
| 4 | 0.018 | 0.982 |
The sigmoid converts an unrestricted score into a value between zero and one, and it never quite reaches either end. A finite score always gives a probability strictly inside the band.
6. Why probability changes unevenly
The amber steps on the sigmoid above are three moves of exactly one unit along the score axis, at −4, at 0 and at 4. Read what each does to the probability.
| from z | to z | from p | to p | change in p |
|---|---|---|---|---|
| −4 | −3 | 0.018 | 0.047 | +0.029 |
| 0 | 1 | 0.500 | 0.731 | +0.231 |
| 4 | 5 | 0.982 | 0.993 | +0.011 |
The same step in the score buys a small change near zero, the largest change near one half, and a small change again near one. The model is linear in its score, and its probability does not move by a constant amount. That is not a flaw to be fixed. A model that has already assigned a 98% chance has less room to become more convinced, and the curve says so.
Part 3. Odds and Log-Odds
7. Probability and odds
A probability of 0.75 is three passes in every four students. Said the other way, it is three passes for every one fail, and that is the odds, three to one.
odds = p / (1 − p)| probability | odds | said aloud |
|---|---|---|
| 0.50 | 1 | one to one |
| 0.75 | 3 | three to one |
| 0.80 | 4 | four to one |
| 0.20 | 0.25 | one to four |
Probability compares the passes with everyone. Odds compare the passes with the fails. They carry the same uncertainty in a different comparison, and the tokens in the widget below are the two comparisons side by side.
8. Log-odds
Odds run from zero up to infinity, and a straight-line score runs from minus infinity to plus infinity, so the two still do not match. Take the logarithm of the odds and they do.
log-odds = ln( p / (1 − p) )A probability below one half has odds below one and negative log-odds. A probability of exactly one half has odds of one and log-odds of zero. A probability above one half has odds above one and positive log-odds. Three scales, one centre.
9. What logistic regression makes linear
Now the defining relationship, which section 4’s score and section 8’s log-odds turn out to be the same number.
ln( p / (1 − p) ) = β·x + αLogistic regression models the log-odds with a straight line. The sigmoid is that relationship solved for p, so the two forms are one statement read in two directions.
p = σ(β·x + α)This is the bridge the page is built on. The model is not linear in the probability, which section 6 showed, and it is not linear in the odds. It is linear in the log-odds, and every reading of a coefficient in the next Part is a reading on that scale.
Part 4. Reading the Fitted Model
10. Reading the intercept
At zero hours the score is α on its own.
z(0) = α p(0) = σ(α) = σ(−7.18) = 0.0008The intercept is the log-odds when the input is zero, and the sigmoid of it is the probability there. For these students that is a chance of passing of eight in ten thousand with no study at all, which is the fitted model’s baseline. Its practical meaning is only as good as zero is as an input.
Nobody here studied zero hours, so the number is the line’s position more than a claim about anyone, exactly as the regression page’s intercept of −68 kg was. Centring the hours would make the intercept the log-odds at the average study time instead.
11. Reading the coefficient
For every additional hour the score rises by β, which is to say the log-odds rise by β, which is to say the odds are multiplied by e^β.
β = 1.69 e^β = 5.44Under the fitted model each additional hour multiplies the odds of passing by about 5.4. The widget below picks a starting time and follows one more hour through all three scales.
| at 3.0 h | at 4.0 h | one more hour | |
|---|---|---|---|
| score z, the log-odds | -2.110 | -0.420 | + 1.690, always β |
| odds | 0.1212 | 0.6570 | × 5.419, always e^β |
| probability p | 0.1081 | 0.3965 | + 0.2884, depends on the start |
One more hour, from two starting points
| 3 h → 4 h | 5 h → 6 h | |
|---|---|---|
| log-odds | −2.10 → −0.40, +1.69 | 1.29 → 2.99, +1.69 |
| odds | 0.123 → 0.670, × 5.44 | 3.65 → 19.8, × 5.44 |
| probability | 0.110 → 0.401, +0.29 | 0.785 → 0.952, +0.17 |
The log-odds change and the odds multiplier are the same from both starting points. The probability change is not.
Keep in mind
A logistic coefficient is a constant change in log-odds and a constant multiplier of odds, and never a constant change in probability. And it is an association in the fitted data. It does not say that making a student study one more hour would multiply their odds by 5.4, only that students who studied an hour more had odds about 5.4 times higher in this sample.
The two sliders in the widget also show what each setting controls. Moving α slides the whole transition left or right, changing the probability at every study time and where the curve crosses one half. Moving β changes how sharp the transition is and, through zero, which way it runs. A positive β rises with x, a negative one falls, and a zero gives a flat line at σ(α).
›How steep the curve is
The probability changes fastest where it is one half. Its slope with respect to study hours combines the fitted score slope with the sigmoid’s derivative.
dp/dx = βp(1 − p)
at p = 0.5: dp/dx = β × 0.5 × 0.5 = β/4
with β ≈ 1.69: dp/dx ≈ 1.69 / 4 ≈ 0.42 per hourThe curve changes more slowly toward either end. Section 24 derives the sigmoid’s derivative.
Check yourself
Questions on the Score, the Odds and the Fit
Question 1 of 5
The score the straight line produces is itself a probability.
Part 5. From Probability to a Classification
12. Choosing a classification threshold
The model can report a probability and stop there, and often that is the right thing to report. When a yes or a no is needed, a threshold t supplies it.
ŷ = 1 when p ≥ t ŷ = 0 when p < tAt t = 0.5 the score threshold is zero, and the boundary on the hours axis is where the line crosses zero. For any other threshold the boundary is where the log-odds reach the threshold’s own log-odds.
boundary x = ( logit(t) − α ) / β logit(t) = ln( t / (1 − t) )
at t = 0.5: logit(0.5) = 0, so x = −α / β = 7.18 / 1.69 = 4.24 hoursThe model estimates a probability. A separate threshold converts that probability into a class, and 0.5 is the conventional choice rather than a mandatory one.
13. Moving the decision boundary
Slide the threshold below. The curve does not move, and nothing is refitted. What moves is the horizontal line, the boundary on the hours axis, and which students land on which side of it.
Large dots are the students at their outcomes, small dots the curve’s probability for them, coloured by the cell they land in.
At 0.5 the model gets ten of twelve right, missing the student who failed after five hours and the one who passed after four. Lower the threshold to 0.3 and the boundary moves to 3.74 hours, the four-hour pass is caught, and accuracy rises to 0.917 with nothing about the curve changed. Raise it to 0.8 and the boundary moves to 5.06 hours, the five-hour fail is caught instead, and two passes are missed. Changing the decision threshold changes the classifications without changing the fitted model.
14. Different errors and different thresholds
Whether 0.3 or 0.8 is better than 0.5 is not a question the model can answer, because it depends on what the two kinds of mistake cost. Suppose the point of the prediction is to offer help to students at risk of failing. Missing a student who needed help is expensive. Offering help to one who did not is cheap. Then the threshold for calling someone a pass should be high, so that more borderline students are flagged as possible fails, at the price of more false alarms.
A useful threshold depends on the consequences of the different mistakes, and choosing it properly belongs to the judging-a-classifier page. What this page needs is the fact that the threshold is a policy decision sitting on top of the probabilities, and not part of the model.
15. Accuracy and the confusion matrix
The four cells under the threshold widget separate the four things that can happen to a student.
| predicted pass | predicted fail | |
|---|---|---|
| actually passed | true positive | false negative |
| actually failed | false positive | true negative |
accuracy = correct predictions / all predictionsClick a cell and the students it counts light up. Two things about accuracy are easy to miss. It evaluates thresholded decisions and says nothing about the probabilities that produced them. And it compresses four kinds of outcome into one number, so a model that misses every rare case can still score well. The section 13 numbers make the first point. Accuracy went from 0.833 to 0.917 while the log loss stayed at 0.2933, because the probabilities never changed.
Part 6. Scoring Probability Predictions
16. Scoring probability predictions
Take a student who passed and two predictions for them, p = 0.9 and p = 0.6. Both become a pass at a threshold of 0.5, so accuracy cannot tell them apart. The first assigned more probability to what actually happened, and it should count as the better prediction. A student who failed, predicted at 0.1 or at 0.4, is the same story from the other side.
Fitting a probability model needs a score that separates predictions which make the same decision at different confidence. Accuracy is not it, and the next three sections build the one that is.
17. Likelihood
For one student, ask how much probability the model gave to the outcome that occurred. If they passed that is p, and if they failed it is 1 − p.
P(yᵢ | pᵢ) = pᵢ when yᵢ = 1
P(yᵢ | pᵢ) = 1 − pᵢ when yᵢ = 0
P(yᵢ | pᵢ) = pᵢ^yᵢ (1 − pᵢ)^(1 − yᵢ) both cases in one lineThe exponent trick is worth reading once. When yᵢ = 1 the second factor has exponent zero and vanishes, and when yᵢ = 0 the first does. Multiply over every student and the result is the likelihood, the probability the model assigned to the complete set of outcomes that happened.
L = ∏ pᵢ^yᵢ (1 − pᵢ)^(1 − yᵢ)For the fitted curve on the twelve students the product is 0.0296. That is small, and it should be, since it is the chance of twelve specific things all happening. What matters is that it is larger than any other curve makes it.
18. Log-likelihood and log loss
Products are awkward to differentiate and quickly become numbers too small to store, so take the logarithm. Products become sums, and the sums are easy.
ℓ = Σ [ yᵢ ln(pᵢ) + (1 − yᵢ) ln(1 − pᵢ) ]Turn it negative and average it, and it becomes a loss to be made small, called the binary log loss.
log loss = −ℓ / n| objective | on the fitted curve | better is |
|---|---|---|
| likelihood L | 0.0296 | higher |
| log-likelihood ℓ | −3.520 | higher |
| log loss −ℓ / n | 0.2933 | lower |
Three readouts of one preference. Whatever curve maximises the first maximises the second and minimises the third, so the fit is the same whichever is used, and implementations generally minimise the third.
19. Confidence and log loss
Per student the loss is −ln(p) for a pass and −ln(1 − p) for a fail, and the two curves say what confidence costs.
| hours | outcome | fitted curve p | assigned to what happened | cost | flatter curve p | cost | same decision? |
|---|
The flatter curve has half the fitted slope and half the fitted intercept, so it crosses one half at the same hours and makes the same twelve decisions.
| observed | predicted p | cost |
|---|---|---|
| pass | 0.9 | 0.105, small |
| pass | 0.6 | 0.511, moderate |
| pass | 0.1 | 2.303, large |
| fail | 0.9 | 2.303, large |
| fail | 0.1 | 0.105, small |
The cost climbs without limit as the curve grows confident in the wrong outcome. In the fitted table the two biggest costs are the two students in the mixed middle, 1.536 for the five-hour fail the curve put at 0.785, and 0.914 for the four-hour pass it put at 0.401. Log loss strongly penalises a confident prediction that assigned little probability to what actually happened.
20. Same accuracy, different probability quality
The second curve in the table above has half the fitted slope and half the fitted intercept. It crosses one half at the same 4.24 hours, so it makes exactly the same twelve decisions and has exactly the same accuracy of 0.833. Its log loss is 0.3425 against the fitted curve’s 0.2933, because it is less sure about the students it is right about, and being less sure costs.
Logistic regression is fitted to probability quality. It maximises the likelihood, and it does not directly choose the boundary that produces the fewest classification mistakes. On these students the two happen to agree. They need not, and section 13 already showed a threshold that beats the fitted curve’s default accuracy without touching its probabilities.
Part 7. Fitting the Model
21. Fitting on the loss surface
Two parameters, α and β. Every pair gives twelve scores, twelve probabilities and one log loss, so the loss is a surface over the pair, and fitting is the fitting-by-walking page’s search across it. The surface below is the log loss over intercept and slope, and the path is the climb itself, recorded pass by pass.
The probability curve at this pass
The walk starts with both coefficients at zero. That assigns a probability of one half to every student, regardless of study hours.
initial log loss = −ln(0.5) = ln 2 ≈ 0.693The optimum on this dataset is near intercept −7.18 and slope 1.69, with loss 0.293. At learning rate 0.5, the first update overshoots and the loss rises to 0.743 before falling. It settles after a few thousand passes. Fitting means finding coefficients whose probability predictions minimize the loss.
22. One optimization pass
A pass here has the same shape as on the walking page with one extra stage, the squash. Begin with the current α and β. Compute each score zᵢ. Convert each to pᵢ. Compute each student’s loss and average. Compute the gradient. Update both coefficients. Draw the new curve.
Scrub the widget above one pass at a time from the start and watch the curve panel. The first few passes lurch, because the rate is bold, and after that the curve steepens steadily into place. Logistic regression repeatedly turns inputs into probabilities, measures their loss, and adjusts its coefficients, and nothing else.
23. The gradient balance
The gradient of the log-likelihood has a form worth remembering, because it looks like something already seen.
∂ℓ/∂α = Σ (yᵢ − pᵢ)
∂ℓ/∂β = Σ (yᵢ − pᵢ)·xᵢThe term yᵢ − pᵢ is the gap between what happened and what the curve expected, a residual on a probability scale. At a finite, unpenalised optimum both sums are zero.
Σ (yᵢ − pᵢ) = 0 Σ (yᵢ − pᵢ)·xᵢ = 0The first says the total of the predicted probabilities equals the number of passes, so the curve is right on average. The second says the gaps also balance after weighting by hours, so the curve is right on average at the low end and the high end separately. The bars below are the gaps on the fitted curve, student by student.
The bars above the line cancel the bars below it almost exactly, which is the balance the derivation sets to zero.
Scrub the passes and the two totals under the bars, which are the two gradient components, shrink toward zero as the climb settles. The regression page’s residuals summed to zero for the same reason, and the gap here is the residual with a probability standing where the prediction stood.
Part 8. Deriving the Gradient
24. Deriving the sigmoid gradient
The derivation needs the sigmoid’s own derivative first, and it is the tidiest in the subject.
›σ′(z) = σ(z)(1 − σ(z))
| Expression | What changed |
|---|---|
| σ(z) = (1 + e⁻ᶻ)⁻¹ | the sigmoid as a power |
| σ′(z) = −(1 + e⁻ᶻ)⁻² · (−e⁻ᶻ) | chain rule through the power and the exponential |
| σ′(z) = e⁻ᶻ / (1 + e⁻ᶻ)² | tidied |
| σ′(z) = [1 / (1 + e⁻ᶻ)] · [e⁻ᶻ / (1 + e⁻ᶻ)] | split into two factors |
| σ′(z) = σ(z) · (1 − σ(z)) | the second factor is 1 − σ(z), since 1 − 1/(1 + e⁻ᶻ) = e⁻ᶻ/(1 + e⁻ᶻ) |
The sigmoid changes fastest when its output is one half. Substitute that output into the derivative to see the maximum slope.
Maximum sigmoid slope = 0.5 × (1 − 0.5) = 0.25The slope approaches zero as the output approaches either extreme. This explains why a saturated sigmoid responds so little to a small change in its input score.
25. Deriving the parameter gradients
Take one student and follow β through the chain to their contribution to the log-likelihood.
ℓᵢ = yᵢ ln(pᵢ) + (1 − yᵢ) ln(1 − pᵢ)
β → zᵢ → pᵢ → ℓᵢ›The chain rule, one student at a time
| Expression | What changed |
|---|---|
| ∂ℓᵢ/∂pᵢ = yᵢ/pᵢ − (1 − yᵢ)/(1 − pᵢ) | differentiate the two logarithms |
| ∂pᵢ/∂zᵢ = pᵢ(1 − pᵢ) | section 24 |
| ∂zᵢ/∂β = xᵢ | the score is linear in β |
| ∂ℓᵢ/∂β = [yᵢ(1 − pᵢ) − (1 − yᵢ)pᵢ] · xᵢ | multiply the three and clear the denominators |
| ∂ℓᵢ/∂β = (yᵢ − pᵢ)·xᵢ | expand the bracket, and everything cancels but the gap |
| ∂ℓᵢ/∂α = yᵢ − pᵢ | the same chain with ∂zᵢ/∂α = 1 |
The messy fractions from the logarithms and the p(1 − p) from the sigmoid cancel each other exactly, and what survives is the gap times the input. Only now sum over the students, and the compact gradient of section 23 is the total of these per-student pieces.
26. Why numerical optimization is required
Set the two gradients to zero and compare with the regression page. There, the prediction was βxᵢ + α, the conditions were linear in β and α, and algebra solved them. Here pᵢ = σ(βxᵢ + α) wraps the coefficients inside an exponential, and the conditions
Σ (yᵢ − σ(βxᵢ + α)) = 0 Σ (yᵢ − σ(βxᵢ + α))·xᵢ = 0cannot in general be rearranged into a formula for β and α. The equations exist, and they are the right ones. They do not have a closed-form solution, and that is why the model is fitted by walking. The fit here climbs the log-likelihood by gradient ascent, which is the same thing as descending the log loss, and reports how many passes it took and whether it met its tolerance.
Part 9. Complete Separation
27. Complete separation
Now twelve students with no mixed middle. Every fail is at three and a half hours or less and every pass at five and a half or more, so a boundary anywhere between them classifies everyone perfectly.
The probability curve at this pass
Scrub the passes and read the three traces together. Accuracy reaches one by pass 100 and never changes again. The slope keeps growing, 1.05 at pass 100, 2.32 at pass 1000, and 3.50 at pass 5000 if the budget allows it. The log loss keeps falling, 0.133, 0.024, 0.006. Every steepening of the curve makes the observed outcomes more likely, since there is no student in the middle to be wrong about, and the climb never finds a top.
Keep in mind
Perfect classification does not guarantee that unpenalised logistic regression has a finite fitted coefficient. On separated data, or on data where one class can be separated on part of the range, the maximum likelihood is only approached and never reached, and what is reported at the end is wherever the pass budget ran out, honestly labelled as not converged. Overlap is not a nuisance to logistic regression. It is what holds the answer finite.
28. Regularizing a separated fit
The repair is the ridge page’s. Add a penalty on the size of the coefficients to the loss, and the climb gains a competing cost. Steepening the curve still raises the likelihood and now also raises the penalty, and at some finite slope the two pulls balance. Penalised logistic regression settles on separated data at a finite, less extreme curve, and the probabilities it reports stay away from zero and one.
In a model
No penalised climb is fitted on this page, so the toggle that would set one beside the unpenalised climb above is not here. What the page shows instead is the pass budget and the honest verdict, which is the minimum a separated fit should come with.
Part 10. What the Probability Means
29. What the predicted probability means
The model estimates a conditional probability.
P(y = 1 | x)For the students that is the estimated chance of passing, given the hours supplied and the fitted model. It does not mean the student is partly passed, or that the model knows the true chance, or that study time is the only thing that matters, or that study time causes the outcome, or that two students at the same hours are in the same circumstances. It is a model-based estimate conditioned on the one feature it was given.
There is also a test the number should pass. If a model assigns many comparable students a probability near 0.8, then about 80 percent of them should turn out to pass. That agreement between stated confidence and observed frequency is calibration, and a model can classify well while being poorly calibrated.
Reading a calibration table takes three steps. Sort the students by the probability the curve gave them and cut that range into bins. Inside each bin, average the predicted probabilities, which is what the curve claimed on average for those students. Then count how many of them actually passed, which is what happened. A calibrated curve has the two columns agree in every bin that holds enough students for a rate to mean anything.
A calibration table on the twelve, which is too few
| probability bin | students | mean predicted p | observed pass rate |
|---|---|---|---|
| 0 to 0.25 | 5 | 0.074 | 0.00 |
| 0.25 to 0.5 | 1 | 0.401 | 1.00 |
| 0.5 to 0.75 | 1 | 0.610 | 1.00 |
| 0.75 to 1 | 5 | 0.924 | 0.80 |
The shape is right at the ends and the middle bins hold one student each, which says nothing. And these are the students the curve was fitted to. Calibration has to be judged on students it never saw.
The lowest bin holds the five students who studied three and a half hours or less. The curve put them at 0.074 on average and none of them passed, so claim and outcome agree. The highest bin holds the five who studied five hours or more. The curve put them at 0.924 on average and four of the five passed, the five-hour fail being the exception, so the curve claimed a little more there than it delivered. The two middle bins are the four-hour pass at 0.401 and the four-and-a-half-hour pass at 0.610, one student each, and a pass rate over one student is not a rate.
Keep in mind
Part 11. Extending to Several Features
30. Extending to several features
So far the score has read one number, the hours. Suppose each student were measured twice, hours studied and, say, hours slept. Each input gets a coefficient of its own, and the score is each input times its coefficient, added up, plus the intercept. That weighted sum is the multiple regression page’s dot product, and everything after it is unchanged.
z = β₁x₁ + β₂x₂ + α p = σ(z)
written for any number of inputs: z = β·x + αThe sigmoid turns the score into a probability exactly as before, the log loss scores that probability against what happened, and the fit is the same walk with one more direction to move in. The gradient gains one component per input, and each component is the gap between outcome and probability times that input, summed over the students. It is the single-input gradient of Part 7 with the second input standing where the hours stood.
∂ℓ/∂βⱼ = Σ (yᵢ − pᵢ)·xᵢⱼ one such sum for each input jEach coefficient changes the log-odds while the other inputs are held fixed, so one more hour of study multiplies the odds by e to the first coefficient among students who slept the same amount, with the same qualification about held-fixed comparisons the multiple regression page gave.
The threshold of one half falls where z = 0, and with two features that is a line across the plane of the two inputs. Every student on one side of it is called a pass and every student on the other a fail, however far from the line they sit. With three it is a plane, and with more it is a flat surface in more dimensions than can be drawn.
boundary at t = 0.5: β₁x₁ + β₂x₂ + α = 0Adding features changes the geometry of the boundary and nothing about the mechanism. The multiclass page takes the next step, from two classes to many.
Check yourself
Questions on Scoring and Fitting
Question 1 of 5
Lowering the threshold from 0.5 to 0.3 moves the boundary to 3.74 hours and raises accuracy from 0.833 to 0.917. What happens to the log loss?
Try it with Botie
Build with Botie
Write and run Python