Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Let's retrace Chapter 6. We started with five people: experience from 1 to 5 years and their salaries. What did we do before training anything?
We made a scatter plot, to check the points roughly followed a straight line.
Right. Then we trained with fit. What did training actually find?
The slope and intercept. For this data that's 6500 and 28500, so Salary = 6500 × Experience + 28500.
And using it for 6 years of experience?
6500 times 6 plus 28500, which is 67,500.
Exactly: dataset, plot, train, interpret, predict. The last step was checking how good the line is.
Our errors are 0, minus 1,500, 2,000, 500 and minus 1,000. How do we get the MSE?
Square each one, add them up to 7,500,000, and divide by 5. So 1,500,000.
That number looks huge. Is the model bad?
Not necessarily. It's in salary squared. The square root is about 1,225, so a typical miss is around 1,200.
Good. And R² compares us with always guessing the average salary of 48,000. Ours is about 0.98. What does that mean?
The line explains about 98 percent of the variation in salary, far better than the average guess.
An R² of 0.98 sounds wonderful. Why shouldn't we celebrate too early?
We scored the line on the same five people it learned from. New people could be different.
Yes, and with only five points, one unusual salary could pull the line a long way. What else?
Predicting for 20 years of experience would be far outside the data, so that's risky too.
Exactly. Next chapter we use the same workflow to predict categories with logistic regression.
So data, train, predict, evaluate stays the same, just with a different kind of output.
A model learns from labeled input-output pairs so that it can predict outputs for new inputs.
Predicts y = mx + c, where m (slope) and c (intercept) are chosen to minimise the squared errors.
Create the dataset, visualise it, train, interpret, predict, plot the line and evaluate.
For the chapter's data, Salary = 6500 × Experience + 28500, which predicts 67,500 for 6 years.
The average squared error, 1,500,000 here; lower is better, and its square root (about 1,225) is the typical miss.
About 0.98 here, meaning the line explains about 98% of the variation in salary compared with always guessing the mean.
MSE is in squared units, so take its square root (RMSE) to read the typical error in salary units.
import math
mse = 1500000.0
rmse = math.sqrt(mse)
print("MSE:", mse, "(in salary units squared)")
print("RMSE:", round(rmse, 2), "(in salary units)")R² compares the model with a baseline that always predicts the average salary of 48,000.
salary = [35000, 40000, 50000, 55000, 60000]
predictions = [35000, 41500, 48000, 54500, 61000]
n = len(salary)
mean_salary = sum(salary) / n
baseline_mse = sum((y - mean_salary) ** 2 for y in salary) / n
model_mse = sum((y - p) ** 2 for y, p in zip(salary, predictions)) / n
print("Baseline (always guess", mean_salary, ") MSE:", baseline_mse)
print("Model MSE:", model_mse)
print("R2 = 1 - model MSE / baseline MSE =", round(1 - model_mse / baseline_mse, 4))Comparing the illustrative line from 6.6 with the least-squares line shows what "best fit" means: least squares has the lower MSE and the higher R².
experience = [1, 2, 3, 4, 5]
salary = [35000, 40000, 50000, 55000, 60000]
mean_salary = sum(salary) / len(salary)
def evaluate(m, c):
predictions = [m * x + c for x in experience]
mse = sum((y - p) ** 2 for y, p in zip(salary, predictions)) / len(salary)
r2 = 1 - sum((y - p) ** 2 for y, p in zip(salary, predictions)) / sum((y - mean_salary) ** 2 for y in salary)
return mse, round(r2, 4)
print("Illustrative line from 6.6 (6250, 28750):", evaluate(6250, 28750))
print("Least-squares line (6500, 28500):", evaluate(6500, 28500))Square every miss and average the lot; keep MSE low, push R² towards one, and your line hits the spot.
Priya trained a line on five salaries and got an R² of 0.98. Delighted, she predicted a salary for 25 years of experience, and her manager laughed: "Your line has only ever seen up to 5 years." Priya learned to test on new data and to stay close to the range she trained on.
"Don't Visit The Island Past Peak Evening" - Dataset, Visualise, Train, Interpret, Predict, Plot, Evaluate.
E.S.A. - Error for each point, Square it, Average them: that is MSE.
Term
What is the equation of simple linear regression, and what do m and c mean?
Click to flip
Term
What line does least squares fit to the chapter's experience and salary data?
Click to flip
Term
What does MSE measure, and is lower or higher better?
Click to flip
Term
What does an R² score of about 0.98 mean?
Click to flip
Term
Why take the square root of MSE?
Click to flip
Term
Why is a high R² on the training data not enough?
Click to flip
A supervised learning algorithm that predicts a number using a straight line, y = mx + c.
The value being predicted, also called the target; here, Salary.
The input used to make the prediction, also called the feature; here, Experience.
How much the prediction changes when the input increases by one; here, about 6,500 per year.
The predicted value when the input is zero; here, 28,500.
Actual value minus predicted value for one example.
The average of the squared residuals; lower is better, and its square root (RMSE) is in the target's units.
One minus the model's squared errors divided by those of always predicting the mean; closer to 1 means more of the variation is explained.
This summary reviews Chapter 6: how linear regression learns a straight line, Salary = m × Experience + c, from labeled examples, and how section 6.9's two metrics, Mean Squared Error and the R² score, tell us how well that line fits.
Chapter 6 used one small dataset, years of experience and salary for five people, to walk through supervised learning with linear regression from start to finish. You created the dataset, plotted it, trained a model with scikit-learn, read its slope and intercept, predicted a salary for 6 years of experience, drew the regression line and evaluated it. Section 6.9 completed the workflow with two metrics: Mean Squared Error (MSE), the average squared prediction error, where lower is better, and the R² score, the share of the variation in salary that the line explains, where closer to 1 is better. For this data the least-squares line is Salary = 6500 × Experience + 28500. It predicts 67,500 for 6 years and scores an MSE of 1,500,000 and an R² of about 0.98.
The big idea. In supervised learning a model learns from labeled examples, inputs paired with correct outputs, so that it can predict the output for new inputs. Linear regression is the simplest supervised algorithm for predicting a number. It models the dependent variable y (the target, here Salary) as a straight-line function of an independent variable x (the feature, here Experience):
y = mx + c
The slope m says how much y changes when x goes up by one. The intercept c is the value of y when x is 0. Training means finding the m and c whose line has the smallest prediction error.
The workflow you followed in this chapter:
X = df[['Experience']] and y = df['Salary'], then model = LinearRegression() and model.fit(X, y).model.coef_[0] is the slope and model.intercept_ is the intercept.model.predict([[6]]) estimates the salary for 6 years of experience.mean_squared_error and r2_score turn "how good is the fit?" into numbers.The whole chapter in one plain-Python script. No libraries are needed to see what those calls do:
# Chapter 6 dataset: years of experience and salary
experience = [1, 2, 3, 4, 5]
salary = [35000, 40000, 50000, 55000, 60000]
n = len(experience)
# Train: least-squares slope (m) and intercept (c)
mean_x = sum(experience) / n
mean_y = sum(salary) / n
m = sum((x - mean_x) * (y - mean_y) for x, y in zip(experience, salary)) / sum((x - mean_x) ** 2 for x in experience)
c = mean_y - m * mean_x
print("Model: Salary =", m, "x Experience +", c)
# Predict
print("Predicted salary for 6 years:", m * 6 + c)
# Evaluate on the training data
predictions = [m * x + c for x in experience]
errors = [y - p for y, p in zip(salary, predictions)]
mse = sum(e ** 2 for e in errors) / n
ss_res = sum(e ** 2 for e in errors)
ss_tot = sum((y - mean_y) ** 2 for y in salary)
r2 = 1 - ss_res / ss_tot
print("Predictions:", predictions)
print("Errors:", errors)
print("MSE:", mse)
print("R2 score:", round(r2, 4))The least-squares line for this data is Salary = 6500 × Experience + 28500. Each extra year of experience adds about 6,500 to the predicted salary, and the line starts from 28,500 at zero years. Section 6.6 used rounder illustrative values (a slope of 6250 and an intercept of 28750) to show how to read the equation; fitting these exact five points gives 6500 and 28500, which is what the code above and scikit-learn both return. The prediction for 6 years is 6500 × 6 + 28500 = 67,500.
Section 6.9: evaluating the model. Evaluation starts from the errors (residuals), actual minus predicted: 0, −1,500, 2,000, 500 and −1,000.
What to remember before trusting a model:
The same workflow, with data, training, prediction and evaluation, carries straight on into Chapter 7, where logistic regression predicts categories instead of numbers.