~/blog
Equation of a Line, 3D Plane, and Hyperplane
You're pricing a house — 1,200 square feet, 3 bedrooms. You know bigger houses cost more, and more bedrooms add value. The question is how to combine these two numbers into a single prediction. The answer is a flat surface in math: a line when you use only square footage, a plane when you add bedrooms. Every linear model you'll encounter — logistic regression, the linear layer in a transformer — is doing the same geometric operation in higher-dimensional space. You need to see that shape clearly before you ever train a weight.
This is not a decision tree, which carves the space into rectangular regions. A hyperplane is a single continuous surface — one formula, no if-else branches.
The Equation of a Line (2D)
With one feature — say, square footage — the prediction is a line:
is the intercept: the value of when . is the slope: how much changes for every one-unit increase in .
For predicting house price from square footage, assume and . Every extra square foot adds $0.20k (i.e., $200) to the predicted price.
| (sq_ft) | residual | |||
|---|---|---|---|---|
| 650 | 50 + 0.20×650 | 180.0 | 180 | 0.0 |
| 850 | 50 + 0.20×850 | 220.0 | 220 | 0.0 |
| 1100 | 50 + 0.20×1100 | 270.0 | 280 | 10.0 |
| 1400 | 50 + 0.20×1400 | 330.0 | 340 | 10.0 |
| 1600 | 50 + 0.20×1600 | 370.0 | 370 | 0.0 |
| 1900 | 50 + 0.20×1900 | 430.0 | 430 | 0.0 |
The two non-zero residuals (at 1100 and 1400 sq ft) tell us the weights aren't quite optimal — but they're close. The goal of training is to find and that minimize the total squared residual.
The slope's sign tells you the direction: means larger houses cost more. would mean the opposite. means the line is horizontal — a feature with no predictive power.
What Changes at 3D: The Equation of a Plane
Add a second feature — number of bedrooms — and the model becomes:
With two features, a single prediction now requires values on two axes, and the model surface is a plane floating in 3D. Assume , , :
| sq_ft | bedrooms | ||||
|---|---|---|---|---|---|
| 650 | 2 | 30 + 110.5 + 30 | 170.5 | 180 | 9.5 |
| 850 | 2 | 30 + 144.5 + 30 | 204.5 | 220 | 15.5 |
| 1100 | 3 | 30 + 187.0 + 45 | 262.0 | 280 | 18.0 |
| 1400 | 3 | 30 + 238.0 + 45 | 313.0 | 340 | 27.0 |
| 1600 | 4 | 30 + 272.0 + 60 | 362.0 | 370 | 8.0 |
| 1900 | 4 | 30 + 323.0 + 60 | 413.0 | 430 | 17.0 |
The residuals are larger than the single-feature case — these particular weights () are illustrative, not optimal. Training will find better values.
Generalizing to p Features: The Hyperplane
With features the model is:
In compact dot-product form, prepend a 1 to each input vector and absorb the intercept into the weight vector:
A hyperplane in dimensions is still a flat surface — it just can't be visualized beyond 3D. The word "hyper" means dimension, not complexity. The relationship is still linear in the parameters.
The Intercept Trick
Without , the hyperplane is forced to pass through the origin. Most real data doesn't pass through the origin — a house with zero square footage doesn't have zero price in the model's internal representation. The standard fix: append a column of ones to the feature matrix.
For the 1-feature anchor, the design matrix with an intercept column is:
The matrix product gives predictions for all six samples at once:
For the 2-feature anchor, expands to 6×3:
The model now holds for all samples simultaneously. This matrix form is how every linear model is implemented at scale — no loops over samples.
Why This Matters for ML
Every linear model is a hyperplane. Logistic regression uses a hyperplane as a decision boundary — points on one side are class 1, the other class 0. SVMs find the hyperplane with maximum margin. The linear layer in a neural network applies this multiplication at each layer. Understanding the geometry now means every subsequent algorithm is just a variation on how the weights are found.
The next question is: which is best? That requires a loss function.
Geometry Summary
| Dimensions | Equation | Geometric Object | Visualizable? |
|---|---|---|---|
| 1 feature | Line (2D) | Yes | |
| 2 features | Plane (3D) | Yes | |
| 3 features | Hyperplane (4D) | No | |
| features | Hyperplane (D) | No |
Related Concepts and Honest Limitations
The design matrix with a leading column of ones is the same representation used to derive the OLS closed-form solution . Understanding why appears there requires exactly the matrix form developed here. The matrix approach also transfers directly to Ridge regression, where the fix is adding to .
A common mistake is assuming more features always make the hyperplane more expressive. The issue isn't expressiveness — it's that when exceeds , the design matrix is rank-deficient and the weight vector is no longer uniquely determined by the data. The hyperplane exists; there are just infinitely many that fit the training data equally well. This is when regularization stops being optional.
Test Your Understanding
-
With and , what is for a house of 1250 sq ft? What is the residual if the true price is $290k?
-
Why does appending a column of ones to allow the model to learn a non-zero intercept? What would happen geometrically if you left it out and the true intercept was $50k?
-
A colleague proposes fitting two separate lines — one for small houses and one for large houses — instead of a single hyperplane. When would this be better, and what model class formalizes that idea?
-
For the 2-feature case, the coefficient means each bedroom adds $15k to price holding sq_ft fixed. How would you confirm this interpretation from the trace table?
-
If you have features and samples, what does the design matrix look like, and why does this cause problems for the OLS formula ?