What machine learning is

scikit-learn · Essentials · 8 min

Every scikit-learn lesson uses the words model, features, target and training; this is where they come from.

Suppose you want a program that spots spam emails. You could write rules by hand: "if the email mentions a prize and has five exclamation marks, it is spam". Rules like that break easily, and there are too many to write. Machine learning takes the other route: you collect many emails that people have already marked as spam or not spam, and a program works out the pattern itself.

The examples the program learns from are the training data: a table of past cases. Each row is one sample (one email). Each column that describes the sample, such as the number of links or whether the sender is in your contacts, is a feature. The column you want to predict, spam or not, is the target, also called the label.

The program's output is a model: a set of learned numbers plus a formula that turns the features of a new, unseen sample into a prediction. Training (in scikit-learn, "fitting") is the step that works those numbers out from the training data. After training, you give the model features only and it predicts the target.

Problems come in two main kinds, and the kind decides which models you can use:

  • Classification: the target is a category, such as spam or not spam, or which of three flower species. The model is called a classifier.
  • Regression: the target is a number, such as a house price or tomorrow's temperature. The model is called a regressor.

Both are supervised learning, because every training sample comes with the right answer. scikit-learn also covers unsupervised learning, where there is no target and the model looks for structure on its own, such as grouping similar customers; that comes later.

scikit-learn is the library that packages all of this: dozens of models, tools to prepare the data and tools to measure how good the predictions are, all used in the same way. You write a few lines of Python; the maths is done for you.

Example

import pandas as pd
from sklearn.tree import DecisionTreeClassifier

# Each row is one sample (a past email); the columns are its features.
emails = pd.DataFrame({
    "links":       [0, 7, 1, 9, 0, 6],
    "exclamation": [1, 5, 0, 8, 2, 4],
    "spam":        [0, 1, 0, 1, 0, 1],   # the target: what we want to predict
})
X = emails[["links", "exclamation"]]     # features
y = emails["spam"]                        # target

model = DecisionTreeClassifier(random_state=0)
model.fit(X, y)                           # training: learn from the examples

new = pd.DataFrame({"links": [8, 0], "exclamation": [6, 1]})
print(model.predict(new))                 # [1 0] -> spam, not spam

Remember

  • Features in, target out: the model learns the link between them from past examples
  • Training (fitting) works out the model's numbers from the training data
  • Category target = classification; number target = regression

Watch out for

  • Expecting a model to know anything it was not shown: it only learns patterns present in its training data.

Check yourself

  1. You have a table of flats with size, floor and district, and the rent each one was let for. You want to predict rent for new listings. Which description fits?
  2. What does training produce?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: scikit-learn.org/stable/getting_started.html