Free courses
Machine LearningBeginnerFree
Machine Learning Foundations: Fit a Line to Data and Test It
Learn how a model learns from data by fitting a simple line to hours studied and exam scores in Python. You will measure error, find the best fit, and check the model on unseen data.
6 lessons 30 minNo account needed
Start lesson 1What you will learn
- Load a small dataset of study hours and exam scores into Python.
- Calculate the mean squared error of a line against the data.
- Fit a line to the training data using least squares formulas.
- Predict an exam score for a new number of study hours.
- Evaluate the fitted model on a separate test set to estimate its performance.
Before you start
- Basic Python: variables, lists, loops, and functions.
- Running a Python script from the command line.
- Understanding of mean and average.
Lessons
- Lesson 1: What is a machine learning model?You load a tiny dataset of study hours and exam scores into Python lists and define a model as a line with slope and intercept that predicts score from hours.6 min
- Lesson 2: Measure how wrong a model isYou write a Python function that computes mean squared error (MSE) for any slope and intercept, and use it to compare a few hand-picked lines against the data.4 min
- Lesson 3: Find the best line by minimising errorYou derive and code the least squares formulas for slope and intercept, then verify that the fitted line has the lowest MSE among all lines.5 min
- Lesson 4: Split data to test fairlyYou divide the dataset randomly into a training set and a test set, fit the line on training data only, and see why evaluating on the same data is misleading.5 min
- Lesson 5: Evaluate your model on unseen dataYou compute the MSE on the test set using the line fitted on training data, and interpret what the error means for future predictions.4 min
- Lesson 6: Put it together: a small prediction toolYou combine the previous steps into one Python script that takes hours studied as input, predicts a score, and reports the test error.6 min