Lesson 19 — Linear regression (closed form)
Fit a straight line with a formula
Linear regression fits a line y = slope × x + intercept through the data. With one feature there is an exact formula, no training loop needed. The slope = Σ(x − mx)(y − my) / Σ(x − mx)², where mx and my are the means. The intercept (where the line crosses x = 0) = my − slope × mx.
# points (1,3) (2,5) (3,7): slope 2, intercept 1 -> y = 2x + 1
Why it matters: linear regression is the simplest predictive model and still used daily for prices, forecasts and trends.
Compute intercept and use the line to predict y for x = 5. Expected: slope 2.0, intercept 1.0, prediction 11.0.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based AI / ML Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for AI / ML Foundations books — check the publication year so the examples match current versions.
Search Amazon →