Math for AI course · Lesson 11 of 180
Linear Equations for AI: Turn One Formula Into a Geometric Line
Learn how one two-variable linear equation becomes a line, test its points, handle vertical cases, and read a linear classifier score contour.
A small classifier assigns a score to two features:
The feature pairs , , and look different, yet each gets the same score:
Those are not three isolated coincidences. They sit on one geometric line, and every point on that line receives score zero. By the end of this lesson, you will be able to turn a two-variable linear equation into a line, produce and test solutions, and recognize the same geometry inside a linear classifier score contour.
Learning outcome: Convert a two-variable linear equation into a line and test solutions.
This lesson builds directly on Lesson 9: Equations for AI, where an equation became a constraint that selects a solution set. Here the allowed values are pairs , and geometry lets us see that set. Lesson 10: Reading AI Derivations supplies the companion habit: every rearrangement must preserve exactly the same points.
The official Mathematics for Machine Learning companion places systems of linear equations at the start of linear algebra. Its Section 2.1 explains the geometric view that one equation in two variables represents a line and that a system asks where such lines intersect. This lesson isolates the one-line foundation. Its examples, code, visual, and assessments are original, and the next lesson will add the second equation.
One equation describes a place
Consider the general two-variable equation
where and are real-valued coordinates and , , and are fixed real coefficients. The equation is linear because and appear only to the first power, are not multiplied by each other, and are multiplied only by fixed numbers.
Suppose . Subtract from both sides, then divide by the known nonzero number :
This is slope-intercept form, . The slope is : increasing by one unit changes by units while the constraint remains true. The vertical-axis intercept is , reached when .
The rearranged formula does more than make plotting convenient. It exposes one free choice. Pick any real ; the equation calculates exactly one matching . As varies continuously, the solution point traces a line rather than a finite collection of dots.
One worked line reveals slope, intercepts, and points
Work through
Here , , and . Because is nonzero, solving for is legal:
The line has slope . If rises by , falls by . That paired change keeps the left side fixed because
A residual turns that truth test into one number:
Points on the line have residual . A nonzero residual gives both a failed test and a sign: positive means the weighted sum is above , and negative means it is below .
| Candidate point | Substitute into | Residual | Verdict |
|---|---|---|---|
| On the line | |||
| On the line | |||
| On the line | |||
| Off the line |
Two distinct points are enough to draw this line, but they are not the whole solution set. The controlled visual makes the missing family tangible.
| Quantity | Current value | How it is obtained |
|---|---|---|
| x-coordinate | 2 | Chosen with the range control |
| y-coordinate | 3 | (12 − 3 × 2) ÷ 2 |
| Residual | 0 | 3 × 2 + 2 × 3 − 12 |
The first server-rendered state is , and the table beneath the chart contains the same calculation. The line does not need JavaScript to make its mathematical point; hydration adds the ability to choose another and watch the matching change. The control uses fixed quarter-unit steps for inspection, while the mathematical parameter can be any real number.
A parameter accounts for the entire line
Writing down several points suggests a pattern, but a parameter describes all of them at once. Let be any real number and set
Every pair in this family is a solution because
The converse matters too. Any pair satisfying must also satisfy . Choosing places that pair in the parameterized family. Together, those two directions show that the parameterization includes every point on the line and no off-line point.
This is why a graphing window never contains the whole mathematical object. Pixels and sampled coordinates are finite; the line is the infinite solution set specified by the equation.
NumPy generates points; the original equation verifies them
The code should mirror the algebra. NumPy documents
linspace
as returning evenly spaced samples over a specified interval. Generate a few
values, calculate their matching values, and then test the original
constraint rather than trusting the rearrangement:
import numpy as np
import matplotlib.pyplot as plt
# 3x + 2y = 12 -> y = (12 - 3x) / 2
x = np.linspace(-2.0, 6.0, num=5)
y = (12.0 - 3.0 * x) / 2.0
residual = 3.0 * x + 2.0 * y - 12.0
print("x:", x)
print("y:", y)
print("residual:", residual)
print("on line:", np.isclose(residual, 0.0, rtol=0.0, atol=1e-12))
candidates = np.array([[2.0, 3.0], [2.0, 4.0], [4.0, 0.0]])
candidate_residual = 3.0 * candidates[:, 0] + 2.0 * candidates[:, 1] - 12.0
print("candidate residual:", candidate_residual)
print(
"candidate on line:",
np.isclose(candidate_residual, 0.0, rtol=0.0, atol=1e-12),
)
plt.plot(x, y, label="3x + 2y = 12")
plt.plot(candidates[:, 0], candidates[:, 1], "o", label="tested points")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.show()
The executed output is:
x: [-2. 0. 2. 4. 6.]
y: [ 9. 6. 3. 0. -3.]
residual: [0. 0. 0. 0. 0.]
on line: [ True True True True True]
candidate residual: [0. 2. 0.]
candidate on line: [ True False True]
np.isclose
compares floating-point values with an explicit tolerance. Here rtol=0.0
and atol=1e-12 mean “accept a residual whose absolute magnitude is at most
.” That is a numerical engineering decision at this example’s scale,
not a symbolic proof that the equation holds. The proof came from the algebra;
the code checks that this implementation agrees.
The plotting call follows Matplotlib’s plot
contract:
the supplied arrays provide the horizontal and vertical coordinates of the
line nodes. A denser plot would look smoother, but it would not create more
mathematical solutions.
A classifier contour is the same equation with feature names
Return to the opening score:
Here and are input features, and are fixed weights, and is an intercept. A score contour collects all feature pairs that receive one chosen score. The zero-score contour satisfies
That is a line. Moving from to raises by and lowers by , leaving the score unchanged:
This form is not invented for the diagram. An official scikit-learn
LinearSVC example
shows the decision function calculated as a feature matrix multiplied by
learned coefficients plus an intercept. The scikit-learn RidgeClassifier
documentation
also states its binary confidence score is proportional to signed distance
from the separating hyperplane and documents that a score above zero predicts
its second class.
For this educational classifier, choose the explicit rule
Under that convention, the zero contour is the decision boundary: crossing it changes the predicted class. Points on one side have a positive score and points on the other have a negative score. The line alone does not specify the tie rule or class names; those belong to the prediction convention. Nor is the raw score automatically a probability.
| Feature pair | Score calculation | Score | Result under the stated rule |
|---|---|---|---|
| Class B, on the boundary | |||
| Class A | |||
| Class B |
Division by zero reveals the missing cases
The sentence “ is a line” needs one condition: and cannot both be zero. Solving for needs the stronger local condition .
| Coefficient case | Solution set | Geometric result |
|---|---|---|
| A nonvertical line | ||
| , | and any real | A vertical line |
| , | and any real | A horizontal line |
| , | Every | The whole plane, not one line |
| , | No solutions | The empty set, not a line |
For example, simplifies to . Every point works, so the line is vertical. Trying to produce by dividing by is not an awkward route—it is undefined. A vertical line has no finite slope because its coordinate does not change while can.
There is one more practical misconception to avoid. Two plotted points can determine a nondegenerate line, but passing two tests does not prove arbitrary code represents the intended equation. A swapped coefficient can still put those particular points in the expected places. Evaluate the residual for several deliberately chosen points, including an off-line point, and retain the algebraic derivation as the source of truth.
One more constraint will turn lines into intersections
One equation leaves one free coordinate: choose , and the line determines —or, for a vertical line, choose while stays fixed. That freedom is why a single equation usually has infinitely many coordinate-pair solutions.
The next lesson will impose a second equation on the same pair. A solution will then need to lie on both lines at once. The point-on-line test developed here becomes an intersection test, and the geometry will reveal why a system can have one solution, no solution, or an entire shared line.
Check your understanding
Question 1
Convert 4x + 2y = 8 into slope-intercept form. State the slope and both axis intercepts, showing each step.
Show the step-by-step solution
Because the coefficient of is the nonzero number , isolate with reversible steps:
Comparing with gives slope and vertical-axis intercept , so one intercept is . For the horizontal-axis intercept, set :
The other intercept is . Both satisfy the original equation: and .
Question 2
Test the points (3, 1), (1, 3), and (0, 4) against 2x + y = 7. Compute a residual for each and classify it as on or off the line.
Show the step-by-step solution
Use the residual :
Only has residual zero, so it is on the line. The other two points are off the line. Their negative residuals mean their weighted sums, and , fall below the required value .
Question 3
Parameterize every solution of x − 3y = 6 using one real parameter, then prove both directions of the set equality.
Show the step-by-step solution
Choose for any . Solving for gives
So the proposed family is
First, every listed pair works because
Second, any solution of the original equation satisfies . Setting writes that solution as . The first direction excludes off-line points; the second ensures no solution was omitted.
Question 4
A student rewrites 5x + 0y = 10 as y = (10 − 5x) / 0 and concludes that the equation has no graph. Diagnose the error and give the correct solution set.
Show the step-by-step solution
The attempted rearrangement divides by the coefficient of , which is zero. Division by zero is undefined, so that step cannot preserve the solution set.
Work from the original equation instead:
The equation places no restriction on . Its solution set is
the vertical line . The graph exists; only slope-intercept form with a finite slope is unavailable.
Question 5
Classify the solution sets of 0x + 0y = 0 and 0x + 0y = 4. Explain why neither equation describes a single line.
Show the step-by-step solution
For every real pair ,
Therefore the first solution set is all of , the entire coordinate plane. It does not select one line.
The second equation simplifies to , which is false for every assignment. Its solution set is , the empty set. It does not contain a line at all. These cases are why the line definition requires at least one of or to be nonzero.
Question 6
For the score s(x₁, x₂) = −x₁ + 2x₂ − 4, find the zero-score contour and classify (0, 3), (2, 3), and (4, 3) using class A for positive scores and class B otherwise.
Show the step-by-step solution
Set the score to zero and isolate :
This line is the zero-score contour. Now evaluate the three points:
Under the stated convention, is class A, while and are class B. The point lies exactly on the boundary. A different tie rule could assign the zero-score point differently, so the equation alone does not determine that label.
Question 7
Debug this NumPy check: on_line = (3 * x + 2 * y == 12). Write a tolerance-aware residual check and explain what it can and cannot establish.
Show the step-by-step solution
Compute the residual first, then compare it with zero using a tolerance chosen for the scale of the calculation:
residual = 3.0 * x + 2.0 * y - 12.0
on_line = np.isclose(residual, 0.0, rtol=0.0, atol=1e-12)This accepts an element when . It avoids demanding bit-for-bit equality after floating-point arithmetic, where rounding may leave a tiny nonzero residual.
The result tests the supplied numerical arrays and this tolerance. It cannot
prove the symbolic identity for every real parameter value, and an
overly large atol could incorrectly accept off-line points. The derivation
establishes the formula; the numerical check helps catch implementation errors.
Sources
- Mathematics for Machine Learning companion website
- Mathematics for Machine Learning book PDF
- NumPy documentation: numpy.linspace
- NumPy documentation: numpy.isclose
- Matplotlib documentation: matplotlib.pyplot.plot
- scikit-learn documentation: RidgeClassifier
- scikit-learn example: Plot the support vectors in LinearSVC