Sub topic 2
Why a Straight Line Is the Wrong Tool for Labels
This chapter assumes you know linear regression, the idea of a loss function, and basic NumPy. You will see why fitting a line to 0/1 labels breaks down. That motivates the model in the next chapters.
The tempting shortcut
Binary labels are numbers, 0 and 1, so you can feed them to ordinary least squares. The fitted value ŷ = w·x + b then acts as a "score". You label an example positive when ŷ ≥ 0.5.
This sometimes gives a usable classifier. It is still the wrong model, for three reasons.
Problem 1: outputs are not probabilities
For a binary label, the expected value of y given x is the probability that y = 1. A linear model tries to estimate that probability. But a line is unbounded, so it can predict -0.4 or 1.7. Neither is a valid probability.
Problem 2: the relationship is not a line
Imagine the chance of passing an exam as a function of study hours. It stays near 0 for very few hours, rises, then flattens near 1. That curve is S-shaped. A straight line cannot follow it, so it fits badly at both ends.
Problem 3: far-away points distort the boundary
Least squares penalizes the distance between ŷ and y. A positive example that is very far from the boundary is already classified correctly. Yet its predicted value overshoots 1, and the squared error pulls the line toward it. The boundary shifts and can misclassify points near the middle.
The next code block shows this. It fits a line to 1D data, then adds one extreme positive example.
import numpy as np
def fit_line(x, y):
A = np.column_stack([x, np.ones_like(x)])
w, b = np.linalg.lstsq(A, y, rcond=None)[0]
return w, b
x = np.array([1.0, 2, 3, 4, 5, 6, 7, 8])
y = np.array([0, 0, 0, 0, 1, 1, 1, 1])
w, b = fit_line(x, y)
print("boundary at x =", (0.5 - b) / w)
x2 = np.append(x, 40.0)
y2 = np.append(y, 1)
w2, b2 = fit_line(x2, y2)
print("boundary with outlier at x =", (0.5 - b2) / w2)
print("prediction at x=40:", w2 * 40 + b2)The first boundary sits at x = 4.5, between the classes. After adding the extreme point, the boundary moves right of 4.5, so the example at x = 4 is still correct but the margin shrinks and x = 5 is closer to being misclassified. The prediction at x = 40 is well above 1.
What we need instead
The fix keeps the linear score but changes what it means. We need a model that:
- Outputs values strictly between 0 and 1.
- Rises smoothly in an S-shape.
- Is not dragged around by points that are already confidently correct.
Linear regression also assumes errors with constant variance. A 0/1 label has variance p(1 − p), which changes with p. This is another sign that least squares does not fit.
The next chapter converts probabilities to odds and log-odds. That gives a scale that is unbounded, so a linear score can live on it.
Exercise
A linear regression on 0/1 labels predicts 1.3 for a new example. Name two reasons this value is a problem.
Show answer
It is not a valid probability, since it exceeds 1. It also shows the model keeps rising past the labels, so extreme points can pull the fitted line and shift the decision boundary.