~/blog

Equation of a Line, 3D Plane, and Hyperplane

Jun 27, 20268 min readBy Mohammed Vasim
Machine LearningAIData Science

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
65050 + 0.20×650180.01800.0
85050 + 0.20×850220.02200.0
110050 + 0.20×1100270.028010.0
140050 + 0.20×1400330.034010.0
160050 + 0.20×1600370.03700.0
190050 + 0.20×1900430.04300.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.

sq_ft price ($k) 600 900 1200 1500 1800 150 200 250 300 350 430 rise=20 run=100 slope=0.20 w₀=50 (x=0) ε=10 ε=10

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_ftbedrooms
650230 + 110.5 + 30170.51809.5
850230 + 144.5 + 30204.522015.5
1100330 + 187.0 + 45262.028018.0
1400330 + 238.0 + 45313.034027.0
1600430 + 272.0 + 60362.03708.0
1900430 + 323.0 + 60413.043017.0

The residuals are larger than the single-feature case — these particular weights () are illustrative, not optimal. Training will find better values.

sq_ft price ($k) beds 9.5 fitted plane ŷ = 30 + 0.17·sqft + 15·beds — residual sticks (point → plane)

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

DimensionsEquationGeometric ObjectVisualizable?
1 featureLine (2D)Yes
2 featuresPlane (3D)Yes
3 featuresHyperplane (4D)No
featuresHyperplane (D)No

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

  1. With and , what is for a house of 1250 sq ft? What is the residual if the true price is $290k?

  2. 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?

  3. 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?

  4. 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?

  5. If you have features and samples, what does the design matrix look like, and why does this cause problems for the OLS formula ?

Comments (0)

No comments yet. Be the first to comment!

Leave a comment