Unit 5 · Tutorial 03
Decision Boundaries
T-02 gave you a probability. To act on it, you need a rule. This tutorial shows how that rule becomes a geometric object — the decision boundary — and why its shape reveals exactly what logistic regression can and cannot learn from your data.
01 Learning objectives
By the end of this tutorial you will be able to
- Convert a predicted probability into a binary decision using a threshold, and explain why the threshold is a separate choice from the model.
- Derive the equation of the decision boundary from a set of logistic regression coefficients.
- Describe the geometry of the boundary — a point in one dimension, a line in two, a hyperplane in higher dimensions — and identify the decision regions it creates.
- Explain why the sigmoid affects the probability field but not the boundary.
- Describe how changing the threshold shifts the boundary parallel, and how changing a coefficient rotates or shifts it differently.
- Explain how feature engineering produces non-linear boundaries while the model remains linear in its parameters.
Position in the unit
T-02 built the probability model; this tutorial turns it into a classifier. T-04 explains how the coefficients are estimated, and T-06 returns to the threshold as a decision-making tool under class imbalance. The geometry developed here also prepares you for the linear classifiers in Module 3 and the layered boundaries of neural networks in Module 5.
02 Motivating example
A hospital is piloting a screening model for a condition that benefits from early detection. Two biomarkers are cheap to measure, and a logistic regression has been fitted to historical patient records:
z = −8 + 0.3·x₁ + 0.5·x₂
where x₁ is the concentration of biomarker A (measured on a scale
from 0 to 25) and x₂ is the concentration of biomarker B (scale
0 to 20). The model outputs the probability that the patient has the
condition.
The clinicians like the probability. It lets them triage — a patient at 0.95 goes straight to confirmatory testing, a patient at 0.05 is reassured and sent home. But at some point the hospital needs an automatic rule. The screening programme runs at scale; not every patient can have a conversation with a specialist before a decision is made.
So the question becomes: where do we draw the line?
The obvious answer is 0.5 — predict positive if the probability is at least half. But this raises a geometric question that turns out to be far more informative than it first appears. In the space of the two biomarkers, the set of patients for whom the model predicts exactly 0.5 is a specific curve. Which curve? What shape does it have? What does its shape tell us about the model?
And a second question follows immediately. If the clinicians decide that 0.5 is too conservative — that they would rather catch more cases at the cost of more false alarms — how exactly does the curve change? Does it tilt? Does it move? Does it become a different shape altogether?
The answers to these questions are what this tutorial is about. And they matter far beyond this one hospital: the shape of the decision boundary is the clearest way to see what any classifier has actually learned.
The question people forget to ask
When someone says "the model predicts the patient has the condition," they are compressing two separate decisions into one sentence: the model's probability output, and the threshold applied to it. These are different things, made by different people, for different reasons. Confusing them is one of the most common sources of trouble in deployed machine learning.
03 Concept explanation
3.1 From probability to decision
A logistic regression outputs p = σ(z), a number between 0 and 1.
To produce a class label, we apply a threshold t:
- Predict the positive class if
p ≥ t. - Predict the negative class if
p < t.
The conventional default is t = 0.5, and in the absence of any
other information it is a reasonable starting point — it treats the two
classes symmetrically. But it is not sacred. In T-06 you will see contexts
where 0.5 is clearly wrong: fraud detection, cancer screening and spam
filtering all call for different thresholds because the costs of the two
kinds of error are not equal.
For now, keep the threshold general. Everything in this section works for any
t.
3.2 The boundary equation
The decision boundary is the set of feature values at which the model is exactly indifferent — the points where the predicted probability equals the threshold.
p = t
Applying the logit to both sides — valid because the logit is a strictly increasing function — gives:
z = logit(t) = log( t / (1 − t) )
Substituting the definition of z:
β₀ + β₁x₁ + β₂x₂ + … + βₚxₚ = logit(t)
This is the equation of the decision boundary. Rearranged:
β₀ + β₁x₁ + β₂x₂ + … + βₚxₚ − logit(t) = 0
The left-hand side is a linear function of the features. The right-hand side is a constant. So the boundary is defined by a linear equation — which is why logistic regression is called a linear classifier, despite its non-linear probability output.
The default case is the simplest
At the default threshold t = 0.5, logit(0.5) = 0,
so the boundary reduces to:
β₀ + β₁x₁ + … + βₚxₚ = 0
Just the linear score set to zero. That is the form you will meet most often, and it is worth remembering as "the boundary is where the score crosses zero."
3.3 Geometry: what shape is the boundary?
The shape of the boundary depends only on how many features the model uses. The table below summarises the possibilities.
| Number of features | Boundary is a… | Equation |
|---|---|---|
| 1 | A single point on the number line | β₀ + β₁x₁ = 0 |
| 2 | A straight line in the plane | β₀ + β₁x₁ + β₂x₂ = 0 |
| 3 | A flat plane in three-dimensional space | β₀ + β₁x₁ + β₂x₂ + β₃x₃ = 0 |
| p | A hyperplane in p-dimensional space | β₀ + Σ βⱼxⱼ = 0 |
The word hyperplane is the general term: a flat surface of one dimension less than the space it lives in. In one dimension it is a point, in two a line, in three a plane, and in higher dimensions the generalisation you cannot visualise but can still work with algebraically.
Whatever its dimension, a hyperplane divides the space into exactly two decision regions — the positive region on one side, the negative region on the other. The model predicts the positive class everywhere in the first region and the negative class everywhere in the second.
3.4 What the sigmoid does and does not control
This is the central insight of the tutorial, so it is worth stating separately.
The sigmoid does not affect the decision boundary
The boundary depends only on the coefficients β₀, β₁, …, βₚ and
the threshold t. It does not depend on which link function
connects z to the probability.
To see why, imagine replacing the sigmoid with any other strictly increasing
function g that satisfies g(0) = 0.5 — the probit
function, for example, or a simple linear ramp clipped to
[0, 1]. The model still predicts positive whenever
g(z) ≥ 0.5. Because g is strictly increasing and
g(0) = 0.5, this happens exactly when z ≥ 0.
The boundary is z = 0 in every case.
What the link function does control is how the probability changes as you move away from the boundary. Near the boundary, the sigmoid rises steeply; far from it, it flattens. A different link function would produce a different probability field while leaving the boundary unchanged.
The practical implication: when you look at a plot of a logistic regression's decision boundary, you are seeing a property of the coefficients, not of the sigmoid. The sigmoid is invisible in that plot.
3.5 Moving the threshold
What happens to the boundary when the threshold changes? Substituting
t = 0.9 into the boundary equation:
β₀ + β₁x₁ + … + βₚxₚ = logit(0.9) = log(9) ≈ 2.197
compared with the default threshold t = 0.5:
β₀ + β₁x₁ + … + βₚxₚ = logit(0.5) = 0
The two equations differ only in the constant on the right-hand side. In geometric terms, the boundary has shifted parallel to itself — same orientation, same slope, different position. Raising the threshold moves the boundary towards the negative region (the model becomes more reluctant to predict positive); lowering it moves the boundary towards the positive region.
Three things do not change when the threshold changes:
- The coefficients. The model is identical. Only the decision rule has changed.
- The probability assigned to each case. Every point in feature space still receives the same predicted probability it did before.
- The ranking of cases. If one case has a higher predicted probability than another before the threshold change, it still does afterwards.
What changes is only the binary label assigned to each case. This is a crucial distinction for T-06, where you will see that threshold tuning is a decision-making activity, not a modelling one.
Threshold change vs. coefficient change
These are often confused. Changing the threshold slides the boundary parallel without altering the model. Changing a coefficient alters the model, which changes the boundary's slope or position — and also changes every predicted probability.
3.6 Non-linear boundaries through feature engineering
So far the boundary has always been a hyperplane. But logistic regression can produce curved boundaries — without changing the model form at all. The trick is to add derived features.
Suppose we add the squares and the product of the two features:
z = β₀ + β₁x₁ + β₂x₂ + β₃x₁² + β₄x₂² + β₅x₁x₂
The model is still linear in its parameters — each βⱼ appears to
the first power — and it can still be fitted by the same machinery as before.
But the boundary is now:
β₀ + β₁x₁ + β₂x₂ + β₃x₁² + β₄x₂² + β₅x₁x₂ = 0
which is the general equation of a conic section. Depending on the coefficients, this can be a circle, an ellipse, a parabola or a hyperbola. The decision regions are no longer half-planes but the inside and outside of a curve.
This is the standard way logistic regression handles problems that are not linearly separable. You are not changing the model family; you are changing the space in which the model operates. T-03's sibling topic in Module 3 — support vector machines — uses the same idea, expressed through kernels rather than explicit features.
3.7 A limitation worth knowing: XOR
Not every problem can be solved this way, and the classic counterexample is worth understanding because it reappears in Module 5.
The XOR problem has four cases and two binary features:
| x₁ | x₂ | Class |
|---|---|---|
| 0 | 0 | negative |
| 0 | 1 | positive |
| 1 | 0 | positive |
| 1 | 1 | negative |
Plot the four points: the two negatives sit at opposite corners of a square,
and the two positives sit at the other two corners. No straight line can put
both positives on one side and both negatives on the other. A logistic
regression on (x₁, x₂) alone simply cannot represent this
function, however long you train it.
But add the product feature x₁x₂ and the problem becomes trivial.
Consider z = x₁ + x₂ − 2x₁x₂ − 0.5:
(0,0):z = −0.5, negative ✓(0,1):z = 0.5, positive ✓(1,0):z = 0.5, positive ✓(1,1):z = −0.5, negative ✓
The lesson generalises. Logistic regression is a linear classifier in the space you give it. If you can supply the right features, it can express highly complex boundaries. If you cannot, it is fundamentally limited — a constraint that motivated the layered architectures you will meet in Module 5.
04 Visual illustration
Figure 1 shows the screening model from Section 02 in the space of its two biomarkers. The straight line is the decision boundary at the default threshold. The shading marks the region where the model predicts the positive class.
Reading the plot
Four things are worth noting.
The boundary is straight. With two features and no derived
terms, the boundary is a line. Points on the line receive
p = 0.5 exactly.
The boundary passes through the middle of the overlap. It is not a perfect separator — some positive cases sit just inside the negative region, and vice versa. That is expected with real data.
Distance from the boundary maps to confidence, but not linearly.
A point just inside the positive region might have p = 0.55; a
point far into it might have p = 0.99. The probability rises
quickly near the boundary and then saturates — the sigmoid behaviour from
T-02.
The boundary is a property of the coefficients, not the data. The points show where the data sits; the line shows what the model has learned. A different model fitted to the same data would draw a different line.
Moving the threshold
Figure 2 shows the same model with three different thresholds. As the threshold rises, the boundary shifts towards the negative region; as it falls, the boundary shifts towards the positive region. In every case the lines remain parallel.
The key observation is that Figure 2 contains exactly one model. The three lines are three different rules applied to the same probabilities. This is why threshold selection belongs to the deployment conversation, not the modelling one.
05 Worked example
Worked example 1 · Deriving and using the boundary
We use the screening model from Section 02:
z = −8 + 0.3·x₁ + 0.5·x₂
Step 1 — write the boundary equation at threshold 0.5.
At t = 0.5, the boundary is where z = 0:
−8 + 0.3x₁ + 0.5x₂ = 0
Step 2 — rearrange into slope-intercept form.
Solving for x₂ makes the geometry explicit:
0.5x₂ = 8 − 0.3x₁
x₂ = 16 − 0.6x₁
The boundary is a straight line with intercept 16 and slope −0.6. In words: at zero biomarker A, the threshold sits at biomarker B = 16. For each additional unit of biomarker A, the threshold for biomarker B falls by 0.6. The two biomarkers are, in the model's view, partial substitutes.
Step 3 — classify five patients.
| Patient | x₁ | x₂ | 0.3x₁ + 0.5x₂ | z | p | Prediction |
|---|---|---|---|---|---|---|
| T | 5 | 5 | 4.0 | −4.0 | 0.018 | negative |
| P | 12 | 8 | 7.6 | −0.4 | 0.401 | negative |
| R | 10 | 10 | 8.0 | 0.0 | 0.500 | on the boundary |
| Q | 12 | 12 | 9.6 | 1.6 | 0.832 | positive |
| S | 20 | 15 | 13.5 | 5.5 | 0.996 | positive |
Patient R sits exactly on the boundary — a coin-flip case. Patients T and P fall in the negative region; Q and S fall in the positive region. Notice how the probabilities spread out as we move away from the boundary: P is close to 0.5, while T and S are near the extremes.
Step 4 — verify one calculation by hand.
For patient Q, x₁ = 12, x₂ = 12:
z = −8 + 0.3(12) + 0.5(12)
= −8 + 3.6 + 6.0
= 1.6
p = 1 / (1 + e^(−1.6))
= 1 / (1 + 0.2019)
= 1 / 1.2019
= 0.832
This matches the table.
Worked example 2 · Moving the threshold
The clinical team reviews the pilot and decides that 0.5 is too conservative.
Missing a genuine case is far more costly than ordering a confirmatory test
that turns out negative. They set the threshold to t = 0.2.
Step 1 — compute the new boundary constant.
logit(0.2) = log( 0.2 / 0.8 )
= log(0.25)
≈ −1.386
Step 2 — write the new boundary.
−8 + 0.3x₁ + 0.5x₂ = −1.386
0.3x₁ + 0.5x₂ = 6.614
x₂ = 13.228 − 0.6x₁
Compare this with the original boundary x₂ = 16 − 0.6x₁. The
slope is identical at −0.6; the intercept has fallen from 16 to 13.228. The
line has slid down and to the left, expanding the positive region.
Step 3 — reclassify the same five patients.
| Patient | 0.3x₁ + 0.5x₂ | p | Prediction at t = 0.5 | Prediction at t = 0.2 |
|---|---|---|---|---|
| T | 4.0 | 0.018 | negative | negative |
| P | 7.6 | 0.401 | negative | positive |
| R | 8.0 | 0.500 | on the boundary | positive |
| Q | 9.6 | 0.832 | positive | positive |
| S | 13.5 | 0.996 | positive | positive |
Patient P flips from negative to positive. Patient T does not — its probability of 0.018 is still well below 0.2. This is the intended behaviour: lowering the threshold catches more borderline cases without labelling every patient positive.
Step 4 — find the threshold that would flip patient T.
Patient T has p = 0.018. To bring it into the positive region,
the threshold would have to fall below 0.018 — an extreme choice that would
classify nearly every patient as positive. This illustrates a practical
point: threshold adjustments have their limits. If the model assigns a very
low probability to a case, no reasonable threshold will rescue it.
Reading the two boundaries together
In slope-intercept form, the two boundaries are
x₂ = 16 − 0.6x₁ and x₂ = 13.228 − 0.6x₁. The
identical slope is the giveaway: only the threshold changed, not the
model. If the slopes had differed, a coefficient would have changed.
06 Interactive activity
Activity · How do the coefficients shape the boundary?
In this activity you will plot the decision boundary for several sets of coefficients and observe how each one changes the line. The goal is to build an intuition that connects algebraic changes in the model to geometric changes in the boundary.
-
Using the code below, generate the boundary for the baseline model
z = −8 + 0.3x₁ + 0.5x₂. -
Change
β₁from 0.3 to 0.6, keeping everything else fixed. How does the line change? -
Restore
β₁ = 0.3and changeβ₂from 0.5 to 1.0. How does the line change now? -
Restore
β₂ = 0.5and changeβ₀from −8 to −4. Describe the change. -
Now set
β₁ = 0. What does the boundary look like? What does the model now say about biomarker A? -
Set
β₂ = 0instead. What happens, and why is this case different from the previous one?
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0, 25, 200)
def boundary(b0, b1, b2, x1):
# b0 + b1*x1 + b2*x2 = 0 -> x2 = -(b0 + b1*x1) / b2
return -(b0 + b1 * x1) / b2
fig, ax = plt.subplots(figsize=(7.5, 5))
configs = [
(-8, 0.3, 0.5, "baseline"),
(-8, 0.6, 0.5, "beta1 doubled"),
(-8, 0.3, 1.0, "beta2 doubled"),
(-4, 0.3, 0.5, "intercept halved"),
]
for b0, b1, b2, label in configs:
ax.plot(x1, boundary(b0, b1, b2, x1), linewidth=2.4, label=label)
ax.set_xlim(0, 25)
ax.set_ylim(0, 20)
ax.set_xlabel("biomarker A (x1)")
ax.set_ylabel("biomarker B (x2)")
ax.set_title("Decision boundaries for varying coefficients")
ax.legend()
ax.grid(alpha=0.25)
plt.tight_layout()
plt.show()
The answers, which you should confirm against your plots:
- Doubling β₁ makes the slope more negative. The line rotates downwards — biomarker A becomes a stronger substitute for biomarker B.
- Doubling β₂ makes the slope less negative. The line rotates the other way — biomarker B becomes more influential relative to biomarker A.
- Halving the magnitude of β₀ shifts the line upward. The model becomes more willing to predict positive at low feature values.
-
β₁ = 0 gives a horizontal line at
x₂ = −β₀/β₂. The model ignores biomarker A entirely — only B matters. -
β₂ = 0 gives a vertical line at
x₁ = −β₀/β₁. Only biomarker A matters. Note that this case is special: settingβ₂ = 0removesx₂from the model, so the "line" no longer depends onx₂at all.
The general rule: coefficients control the orientation of the
boundary; the intercept and threshold control its position. More
precisely, the vector (β₁, β₂, …, βₚ) is perpendicular to the
boundary, so changing a coefficient tilts it, while changing
β₀ or t slides it.
07 Self-check questions
Answer each question before revealing the solution. If you get one wrong, re-read the relevant subsection rather than simply moving on.
Question 1
State the decision boundary equation for a general logistic regression at the default threshold. What does it reduce to?
Show answer
The boundary is the set of points where the predicted probability
equals the threshold. At t = 0.5, this is where
z = 0, so the equation is
β₀ + β₁x₁ + … + βₚxₚ = 0. It reduces to the linear
score being zero.
Question 2
A model has two features. What shape is the decision boundary? What if it has three features?
Show answer
With two features the boundary is a straight line in the plane. With
three features it is a flat plane in three-dimensional space. In
general, with p features it is a hyperplane of dimension
p − 1.
Question 3
Two logistic regression models are fitted to the same data. One uses the sigmoid link; the other uses the probit link. Do they have the same decision boundary? Explain.
Show answer
Not necessarily, and the answer depends on the threshold. Both link
functions are strictly increasing and both map 0 to 0.5, so at
t = 0.5 both boundaries are z = 0 — though
the coefficients will differ because the two models are fitted
differently.
At a threshold other than 0.5, the boundaries will generally differ,
because logit(t) ≠ probit(t) for t ≠ 0.5.
The two link functions assign different probabilities to the same
score, so the level sets differ.
Question 4
A model has z = 2 − x₁ + 3x₂. Write the equation of the
decision boundary at t = 0.5, and state which side is the
positive region.
Show answer
Setting z = 0: 2 − x₁ + 3x₂ = 0, or
rearranged, x₂ = (x₁ − 2) / 3.
The positive region is where z > 0, i.e.
2 − x₁ + 3x₂ > 0, which is above and to the left of the
line in the standard (x₁, x₂) plane — because increasing
x₂ raises z, while increasing x₁
lowers it.
Question 5
You raise the threshold from 0.5 to 0.8. Describe in one sentence what happens to the decision boundary and what happens to the predicted probabilities.
Show answer
The boundary shifts parallel towards the negative region — the model becomes more reluctant to predict positive — while every case's predicted probability remains exactly what it was. Only the classification rule has changed.
Question 6
Can a logistic regression produce a circular decision boundary? If so, how?
Show answer
Yes, by adding quadratic features. If the model includes
x₁² and x₂² as inputs, the boundary equation
becomes quadratic, and its solution set can be a circle or ellipse.
Crucially, the model is still linear in its parameters — the
βⱼ still appear to the first power — so the same fitting
procedure applies. Only the geometry of the decision surface changes.
08 Practice exercises
Work these by hand first, then check your answers computationally.
-
A model has
z = −4 + 0.5x₁ + 0.25x₂. Write the boundary equation att = 0.5in slope-intercept form (x₂as a function ofx₁). -
For the same model, classify the points
(5, 7),(2, 5)and(8, 3), and compute the predicted probability for each. -
Using the model from exercise 1, find where the boundary crosses the
x₁-axis and where it crosses thex₂-axis. -
A model has
z = −6 + x₁ + x₂. Find the boundary equation att = 0.9. -
For the model
z = −5 + 0.4x₁ + 0.5x₂, what threshold would place the point(5, 3)exactly on the boundary? -
A logistic regression has
β₁ = 0. Is the decision boundary vertical, horizontal, or neither? Explain your reasoning, then answer the same question forβ₂ = 0. -
A model has
z = −2 + 0.1x₁ + 0.1x₂ + 0.02x₁² + 0.02x₂². Is the boundary linear? What shape is it likely to be, and why? -
A model's boundary at
t = 0.5isx₂ = 10 − 2x₁. Sketch the positive and negative regions and mark one point on each side that is far from the boundary. What are the approximate probabilities at those two points, given that the model's score at a distancedperpendicular to the boundary grows proportionally tod?
09 Problem sets
These problems require reasoning about model behaviour rather than computation. Several have more than one defensible answer; what matters is the quality of the argument.
Problem 1 · Reading a boundary as a story
A clinical model produces the boundary x₂ = 15 − 0.5x₁ at
threshold 0.5, where x₁ and x₂ are two biomarkers.
Describe in plain language what the model has learned about how the two
biomarkers relate. Under what circumstances would a patient be predicted
positive? What would you want to check before trusting this boundary?
Problem 2 · The threshold as a policy choice
Two clinicians disagree about the threshold for a screening model. One argues for 0.3; the other for 0.7. What is each of them optimising for? Express your answer in terms of false positives and false negatives, and explain why the "correct" threshold cannot be determined from the data alone.
Problem 3 · Why the sigmoid does not matter
Prove that any strictly increasing link function g satisfying
g(0) = 0.5 produces the same decision boundary at threshold
0.5 as the sigmoid, for a model of the form
z = β₀ + β₁x₁ + ….
Problem 4 · Parallel boundaries
Model A has the boundary 2x₁ + x₂ = 6. Model B has the boundary
2x₁ + x₂ = 9. What is the relationship between the two
boundaries? What could account for the difference — a change in the
coefficients, a change in the threshold, or both? Construct two distinct
explanations and explain how you would tell them apart from the models'
other outputs.
Problem 5 · Does standardising features change the boundary?
Suppose you standardise your features before fitting — subtracting the mean and dividing by the standard deviation of each feature. Does the decision boundary change? Are the predicted probabilities the same? Are the predictions the same? Justify your answer algebraically.
Problem 6 · XOR revisited
Write out the four points of the XOR problem. Show that no straight line can
separate the positives from the negatives. Then show that adding the feature
x₁x₂ makes separation possible, by constructing an explicit set
of coefficients that works.
10 Discussion and projects
Discussion questions
- Is the decision boundary a property of the model or of the decision-maker? The coefficients belong to the model; the threshold belongs to whoever deploys it. Where does the boundary itself belong?
- Logistic regression produces a linear boundary. Is this best understood as a limitation or as a feature? What does it buy you that a more flexible model would not?
- A point far from the boundary has a predicted probability close to 0 or 1. Does this mean the model is more likely to be correct about that point? Discuss, considering the possibility that the model itself is wrong.
- In medical screening, would you rather move the boundary towards the positive region or the negative region? Does the answer depend on the disease, the treatment, and the patient? Construct two contrasting cases.
Mini-project · Visualising decision boundaries in scikit-learn
In this project you will fit a logistic regression to a two-dimensional dataset and plot both the decision boundary and the surrounding probability field. The visual is one of the most useful diagnostic tools in applied machine learning, and it directly connects the algebra of this tutorial to the geometry of a real dataset.
Use make_classification with two features and a moderate class separation.
Use LogisticRegression from scikit-learn and inspect the coefficients.
Create a dense grid of points covering the feature space and predict the probability at each.
Use contourf to shade the space by predicted probability.
Overlay the contour at p = 0.5. Is it straight? Why must it be?
Add degree-2 polynomial features and refit. How does the boundary change?
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.preprocessing import PolynomialFeatures
X, y = make_classification(n_samples=250, n_features=2, n_redundant=0,
n_informative=2, n_clusters_per_class=1,
class_sep=1.1, random_state=0)
clf = LogisticRegression().fit(X, y)
xx, yy = np.meshgrid(
np.linspace(X[:, 0].min() - 0.8, X[:, 0].max() + 0.8, 300),
np.linspace(X[:, 1].min() - 0.8, X[:, 1].max() + 0.8, 300),
)
grid = np.c_[xx.ravel(), yy.ravel()]
Z = clf.predict_proba(grid)[:, 1].reshape(xx.shape)
fig, ax = plt.subplots(figsize=(7.5, 5.5))
contour = ax.contourf(xx, yy, Z, levels=20, cmap="Blues", alpha=0.75)
ax.contour(xx, yy, Z, levels=[0.5], colors="#1f4e8c", linewidths=2.6)
ax.scatter(X[y == 0, 0], X[y == 0, 1], marker="o", facecolors="none",
edgecolors="#5b6472", s=45, linewidths=1.6, label="negative")
ax.scatter(X[y == 1, 0], X[y == 1, 1], marker="s", c="#0f7b6c",
s=42, label="positive")
ax.set_xlabel("feature 1")
ax.set_ylabel("feature 2")
ax.legend(loc="upper left")
plt.colorbar(contour, ax=ax, label="predicted probability")
plt.tight_layout()
plt.show()
As you work through the project, keep four questions in mind. Is the boundary a straight line, and why must it be? What do the contour colours actually represent? Are there misclassified points, and what are their predicted probabilities? And when you add polynomial features, does the boundary remain linear — and if it becomes curved, has the model become non-linear?
11 Summary
This tutorial turned the probability model of T-02 into a classifier, and examined the geometry that results.
-
A classifier is a probability plus a threshold. Predict
positive when
p ≥ t. The default is 0.5, but the choice of threshold is a decision-making problem, not a modelling one. -
The decision boundary is where
p = t. Equivalently,β₀ + β₁x₁ + … + βₚxₚ = logit(t). At the default threshold this reduces to the linear score being zero. - The boundary is a hyperplane. A point in one dimension, a line in two, a plane in three, and the generalisation in higher dimensions. It divides the feature space into exactly two decision regions.
-
The sigmoid does not affect the boundary. Any strictly
increasing link function with
g(0) = 0.5produces the same boundary at the default threshold. The link function shapes the probability field, not the decision surface. - Changing the threshold moves the boundary parallel. The coefficients, the probabilities and the ranking of cases are all unchanged; only the labels change.
- Changing a coefficient rotates or shifts the boundary differently. The coefficient vector is perpendicular to the boundary, so altering it tilts the surface rather than sliding it.
- Non-linear features produce non-linear boundaries. Adding squares or products of the original features makes the boundary a conic section, while the model remains linear in its parameters.
- Not every problem is linearly separable. XOR cannot be solved by logistic regression on its raw features. This limitation is the starting point for the layered architectures in Module 5.
Before moving to T-04
You should be able to take any set of logistic regression coefficients, write down the decision boundary at any threshold, sketch the two decision regions, and explain what changes — and what does not — when the threshold is adjusted. You should also be able to say why the boundary is linear despite the sigmoid being a curve.
12 Further reading
Core
- James, Witten, Hastie & Tibshirani — An Introduction to Statistical Learning, Chapter 4. Section 4.3 covers logistic regression, and Section 4.4 on linear discriminant analysis provides a useful contrast: a different model form that produces the same family of linear boundaries.
- Hastie, Tibshirani & Friedman — The Elements of Statistical Learning, Section 4.4. A more rigorous treatment, including the connection to generalised linear models and to the geometry of separating hyperplanes.
- scikit-learn documentation — LogisticRegression, and the example gallery entries on decision boundaries. The gallery contains several worked examples plotting decision boundaries for different classifiers on the same data — a useful side-by-side comparison.
Supplementary
- Bishop — Pattern Recognition and Machine Learning, Chapter 4. A probabilistic treatment that develops the geometry of linear decision boundaries in detail and connects them to the broader family of linear models for classification.
- Duda, Hart & Stork — Pattern Classification, Chapters 4–5. The classic treatment of linear discriminants and the geometry of decision surfaces, with extensive discussion of the XOR problem and its significance.
- Provost & Fawcett — Data Science for Business, Chapter 7. An accessible discussion of threshold selection as a business decision, with concrete examples from fraud detection and direct marketing.
On the history and limits of linear separability
- Minsky & Papert — Perceptrons (1969). The book that famously demonstrated the limits of single-layer linear classifiers, using XOR as the central example. Its influence — and the subsequent revival of neural networks — is essential context for Module 5.
- Cover, T.M. — "Geometrical and Statistical Properties of Systems of Linear Inequalities with Applications in Pattern Recognition" (1965). The classic paper on the capacity of linear classifiers in high-dimensional spaces, and why the XOR limitation fades as the number of features grows.