BeginnerCoder

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.

Want it another way?
Task

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.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with AI / ML Foundations

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.