Module 10 · Machine learning: linear regression · Lesson 44 of 66

What is machine learning?

How a model learns from examples, the main kinds of learning, and why we test on data it hasn't seen.

In this lesson · about 10 minutes

  • Tell supervised, unsupervised and reinforcement learning apart
  • Explain why we split data into a training set and a test set
  • Know the four steps every scikit-learn model follows

Machine learning is a way of building programs that learn patterns from data instead of following rules someone wrote by hand. Show a model thousands of past house sales, and it learns how size, location and age relate to price. Then it can estimate the price of a house it has never seen.

Three kinds of learning

  • Supervised learning: every example comes with the right answer, called the label. The model learns to predict the label from the other columns, the features. Predicting a number (a price) is regression; predicting a category (spam or not) is classification. Most of this course is supervised learning.
  • Unsupervised learning: no labels. The model looks for structure on its own, like groups of similar customers (clustering).
  • Reinforcement learning: an agent learns by trial and error from rewards, like a program learning to play a game.

Train, then test

A model can simply memorize its examples, which tells you nothing about how it will do on new data. So we hold some data back. The model learns from the training set, and we measure it on the test set, which it never saw. If it does well there, it has learned something real.

In scikit-learn, Python's main machine learning library, every model follows the same four steps:

  1. Import the model class: from sklearn.linear_model import LinearRegression
  2. Create it, with any settings: model = LinearRegression()
  3. Fit it to the training data: model.fit(X_train, y_train)
  4. Predict on new data: model.predict(X_test)

By convention X (capital) holds the features, a table, and y holds the labels, one column. Learn this pattern once and you can use any model in the library.

Splitting data

train_test_split shuffles the rows and splits them. random_state fixes the shuffle so you get the same split every time.

Check your understanding0 of 2 correct

Question 1

1.A model predicts tomorrow's temperature in degrees. What kind of task is that?

Question 2

2.Why do we keep a test set the model never sees during training?

Quiz score: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.