LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 35 lessons

Lesson 1 of 9

Module 4 · Machine learning

What machine learning is

What machine learning is

What machine learning is

A model learns a pattern from examples, then applies it to cases it has never seen. This lesson sets out the vocabulary and meets the first dataset.

⏱ 12 min readModule 4 · Machine learningFree
Lesson at a glance3 steps · about 12 min
  1. The vocabulary
  2. When not to use machine learning
  3. The Wisconsin breast cancer dataset

The first three modules gave you the three things this one needs. From Python basics you can load, clean and combine a table with pandas. From Figures that tell the truth you can look at data honestly before trusting a summary. From Coding with AI assistants you have the habit of reading code before running it and testing what it does. Machine learning uses all three: a model is only as good as the table it learns from, the plots you make of it, and the checks you run on it.

The vocabulary

Machine learning means writing a program that finds a rule from examples, instead of a person writing the rule by hand. The table below gives the words you will meet in every lesson of this module.

TermMeaning
SampleOne example, such as one patient or one biopsy: a row.
FeatureA measurement used to predict: a column, such as tumour radius.
Label (target)The answer to predict, such as malignant or benign.
Supervised learningLearning from examples with labels: classification (a category) or regression (a number).
Unsupervised learningFinding structure without labels, such as clusters.
Training and test setsData the model learns from, and data held back to check it.
OverfittingLearning the noise in the training data, so the model fails on new data.

How to read this table

The first three rows describe the data; the rest describe the learning. A dataset for machine learning is a table: each row is a sample, each column a feature, and one extra column holds the label. In Python, the features are usually stored as X (a capital letter, because it is a whole table) and the labels as y (a single column).

  • Classification and regression. If the label is a category, such as malignant or benign, the task is classification. If it is a number on a scale, such as a disease score, it is regression. Your first classifier to Trees and ensembles classify; Regression: predicting a number does regression.
  • Unsupervised learning. Here there is no label column at all. In single-cell RNA-seq, for example, thousands of cells are grouped by which genes they use, and the groups are later named as cell types. Unsupervised learning covers this.
  • Training and test sets. The test set plays the part of future patients. If the model has seen it during training, it is no longer a fair check.
  • Overfitting. Think of a student who memorises last year's exam answers. They score perfectly on that paper and badly on a new one. A model that fits the training data too closely does the same.

Two more words complete the set. The model is the rule that the program finds, stored as numbers inside a Python object. Fitting (or training) is the step that finds those numbers from the training data. In scikit-learn every model works the same way: you create it, call .fit(X, y) on the training data, then call .predict(X) on new rows to get its guesses. Once you know that pattern, every model in this module reads alike.

A common pitfall is a feature that is really the label in disguise. If a hospital table has a column such as "date of chemotherapy", a model will learn that patients with a chemotherapy date have cancer. It scores well and is useless, because the column only exists after the diagnosis. Before you fit anything, ask of every feature: would I know this value at the moment I want to make the prediction?

When not to use machine learning

A model needs many examples. An RNA-seq study with 8 samples and 20,000 genes is the wrong shape: any model would memorise the 8 samples and nothing would be left to test it on. A statistical test is the right tool there. Knowing when not to use machine learning is part of using it well.

Why is that the wrong shape? With 20,000 features and 8 rows, there are far more ways to draw a dividing line than there are samples to check it against. Some combination of genes will always separate the 8 samples perfectly, by chance alone. A statistical test asks a narrower question, gene by gene, such as "is this gene's average different between the two groups?", and it comes with a known way to control false positives. Machine learning starts to make sense when you have hundreds of samples, and preferably more samples than features.

Sample size is not the only reason to hold back. Machine learning predicts; it does not explain why. If your question is "does this drug lower blood pressure?", you need a controlled comparison, not a model that predicts blood pressure well. A model can predict accurately from features that are only linked to the outcome by chance or by a shared cause. It is also a poor choice when a simple, known rule already works, such as a clinical cut-off on one blood test, or when the labels themselves are unreliable. A model trained on wrong labels learns to repeat the mistakes.

Go deeper: how few features it takes to fool you

There is an exact result behind the 8-sample warning. A straight-line rule, or with more features a flat dividing surface, can split points in d features into any two groups you like, as long as there are no more than d + 1 points and they are in “general position” (with two features, for example, no three points on one line). This goes back to Cover (1965) in IEEE Transactions on Electronic Computers.

So with 8 samples, just 7 features are enough to fit any labelling perfectly, including labels assigned by a coin toss. With 20,000 features, a perfect fit tells you nothing at all. This is one face of the curse of dimensionality: the more features you have, the more ways there are to find a pattern that exists only by chance.

The Wisconsin breast cancer dataset

Each of the 569 rows is a breast mass sampled by fine-needle aspiration. A digitised image of the cells gave 30 measurements of the cell nuclei, such as radius, texture and concavity. Each mass was diagnosed malignant or benign. It ships with scikit-learn.

Fine-needle aspiration draws a small number of cells out of a lump through a thin needle. The cells are spread on a slide, stained and photographed, and the outline of each nucleus is traced on the image. Ten characteristics were measured for every nucleus, such as radius, texture (how much the grey level varies inside it) and concave points (a measure of the inward dents in its outline). Each characteristic is then summarised three ways across the nuclei in the image: the mean, the standard error (named "error" in the column names) and the "worst" value (the average of the three largest). Ten characteristics × three summaries = 30 features. scikit-learn is the main Python library for machine learning, and it includes this dataset so that you can load it without downloading anything.

What machine learning is · LSMA free course A table with one row per breast mass, 569 rows in all. Thirty feature columns, such as mean radius and mean texture, form X. A separate diagnosis column, malignant or benign, is the label y. ONE ROW PER MASS, ONE COLUMN PER MEASUREMENT mean radius mean texture … 27 more worst texture diagnosis malignant benign benign malignant ⋮⋮⋮⋮⋮ 569 rows: samples X · 30 features y · the label
Every row is one breast mass. The grey bars stand for measured numbers; the model learns from the 30 feature columns (X) to predict the diagnosis column (y). Schematic: only four of the 569 rows and four of the 30 feature columns are drawn.
Python
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer

data = load_breast_cancer(as_frame=True)
df = data.frame
df["diagnosis"] = df["target"].map({0: "malignant", 1: "benign"})
print(df.shape)
print(df["diagnosis"].value_counts())

cols = ["mean radius", "mean texture", "mean concave points"]
print(df.groupby("diagnosis")[cols].mean().round(2))

What this does

  • The first two lines load NumPy (fast arithmetic on arrays of numbers) and pandas (tables), under the short names np and pd that almost all Python code uses. The third line imports the function that loads this dataset from scikit-learn, whose package name is sklearn.
  • load_breast_cancer(as_frame=True) loads the data. The argument as_frame=True asks for pandas tables rather than bare arrays, so every column keeps its name. The result, data, is a bundle that holds the table plus extras such as the list of feature names.
  • df = data.frame takes the full table: the 30 feature columns plus a column called target that holds the diagnosis as a number.
  • scikit-learn codes the diagnosis as 0 for malignant and 1 for benign. The next line uses .map() to translate each number into a word, using the dictionary {0: "malignant", 1: "benign"}, and stores the words in a new column, diagnosis.
  • df.shape gives the number of rows and columns. value_counts() counts how many times each diagnosis appears.
  • The last two lines pick three columns, split the rows into the two diagnosis groups with groupby, take the mean of each column in each group, and round to two decimal places.
(569, 32) diagnosis benign 357 malignant 212 Name: count, dtype: int64 mean radius mean texture mean concave points diagnosis benign 12.15 17.91 0.03 malignant 17.46 21.60 0.09

Reading the output

  • (569, 32): 569 rows and 32 columns. The 32 are the 30 features, the original target column and the new diagnosis column. Only the 30 features will be used to predict.
  • The counts: 357 benign and 212 malignant, which add up to 569. The line Name: count, dtype: int64 is pandas describing the result: it is called "count" and holds whole numbers (64-bit integers).
  • The last table has one row per diagnosis and one column per chosen measurement. On average, malignant nuclei have a larger radius (17.46 against 12.15), a higher texture value (21.60 against 17.91) and a clearly higher concave points value (0.09 against 0.03).

Notice two things before going further. First, the coding: malignant is 0 and benign is 1, which is the opposite of what many people expect. When you later count "positives", check which number stands for cancer, or your sensitivity will quietly become the sensitivity for benign masses. Second, the scales: radius is in the tens and concave points in hundredths. Some models are thrown by features on very different scales, which is why the next lesson rescales them.

A mean is a summary, and in Showing distributions you saw what summaries hide. Before modelling, plot at least one feature by group. The code below, using the seaborn library from the earlier modules, draws one histogram of mean radius per diagnosis on the same axes.

Python
import matplotlib.pyplot as plt
import seaborn as sns

sns.histplot(data=df, x="mean radius", hue="diagnosis", bins=30)
plt.show()

The point of the plot is the overlap. Large radii are almost all malignant and small ones mostly benign, but there is a middle range where both groups are common. That middle range is where a model's mistakes will happen, and it is why combining several features helps.

Malignant nuclei are larger and more irregular on average, but the groups overlap, so no single measurement separates them. A model that combines all 30 can do better. The classes are also imbalanced: 37% malignant. A model that always said “benign” would already be 63% accurate, which is why accuracy alone is a weak measure.

Two of these terms are worth fixing now. Accuracy is the share of all predictions that are correct. Imbalanced classes means one label is much more common than the other: here 212 ÷ 569 = 37% malignant. Imbalance is normal in medicine, where most people screened for a disease do not have it, and it is exactly when accuracy misleads most. A test for a condition that affects 1 in 100 people scores 99% accuracy by declaring everyone healthy.

Worked example · the baseline to beat

always predict “benign” → 357 ÷ 569 = 62.7% accuracy
and it finds 0 of 212 cancers: sensitivity 0%

Always work out what a trivial rule achieves. A model is only useful if it clearly beats it on the measure that matters.

Step by step. The rule labels all 569 masses benign. It is right for every benign mass, so it gets 357 correct. Accuracy = correct ÷ total = 357 ÷ 569 = 0.627, or 62.7%. Sensitivity is the share of real cancers that a rule catches. There are 212 cancers and the rule calls none of them malignant, so sensitivity = 0 ÷ 212 = 0%.

What the answer means. A score of 62.7% is the floor, not a result. Any model you build must clearly beat it, and for a cancer test the measure that matters most is how many of the 212 cancers it finds. Evaluating a classifier returns to this with the full set of measures.

The dataset comes from Street, Wolberg and Mangasarian (1993), “Nuclear feature extraction for breast tumor diagnosis”, in the proceedings of the IS&T/SPIE Symposium on Electronic Imaging. The samples were collected at the University of Wisconsin Hospitals, Madison, and the data are also available from the UCI Machine Learning Repository as Breast Cancer Wisconsin (Diagnostic).

Two cautions when citing it. First, an older Wisconsin breast cancer dataset exists, with 699 samples and nine features scored by eye, so name the diagnostic version with 569 samples. Second, these are single-centre data from the early 1990s. Very high scores on them are common and do not show that a model would work in a clinic today.

Key point

A model is a summary of its training data. If the data are biased, small or from one hospital, the model will be too, however good its score looks.

A second example: a model trained on slide images from one hospital's scanner can pick up that scanner's colour and focus as clues. On images from another hospital, with different staining and a different scanner, its accuracy can fall sharply. This is why a model needs testing on data from a new site before anyone relies on it.

Further reading · Module 4Chicco 2017: ten practical tips for machine learning in biology. · All reading for this course

Check your understanding

1. Predicting a patient's blood pressure from their age and weight is:

2. In this dataset, what is one sample?

3. Why is 63% accuracy not impressive here?

Up next

Lesson 2

Your first classifier

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.