Skip to content
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 1

What 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
1,664 open machine learning engineer roles on Rolebound right nowSee the roles

Ready to use this in a job?

Rolebound applies to matching roles for you.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Machine Learning Foundations: Fit a Line to Data and Test It (free course) | Rolebound