Lesson 1 of 13 · 10 min
What ML Is (and Isn't) in Biology
Machine learning fits a predictive function directly from examples instead of encoding that function as explicit rules a human wrote. In biology this matters because the mapping from sequence, structure, or expression to phenotype is high-dimensional and only partially understood, so a model estimated from data can capture interactions no analyst would enumerate by hand. What it is not: a source of mechanism. A fitted model gives you a mapping that generalizes, not a causal explanation of why that mapping holds.
Supervised vs Unsupervised
Supervised learning trains on inputs paired with known targets and optimizes to predict those targets on unseen inputs. Classifying a missense variant as pathogenic or benign from features like CADD phred score, phyloP conservation, and gnomAD allele frequency is the canonical case. Unsupervised learning receives no labels and instead recovers structure such as clusters or a low-dimensional manifold. Grouping cells by transcriptional state from a single-cell RNA-seq count matrix is the archetype, because the cell types are not known in advance and there is no target column to fit against.
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
# Toy variant table. Columns: CADD_phred, phyloP, gnomAD_AF
X = np.array([
[32.0, 7.9, 1e-6],
[28.5, 5.2, 3e-6],
[30.2, 6.5, 2e-6],
[26.8, 4.8, 5e-6],
[34.1, 8.3, 1e-6],
[24.5, 3.9, 9e-6],
[ 3.1, -0.4, 0.12],
[ 1.8, 0.1, 0.30],
[ 2.5, -1.2, 0.08],
[ 4.0, 0.3, 0.20],
[25.0, 4.1, 8e-6], # conserved site but curated benign
[ 5.2, 0.6, 0.05],
])
y = np.array([1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]) # 1 = pathogenic, 0 = benign
clf = RandomForestClassifier(n_estimators=200, random_state=0)
scores = cross_val_score(clf, X, y, cv=4)
print(f"cv accuracies: {np.round(scores, 2)}")
print(f"mean accuracy: {scores.mean():.2f}")cv accuracies: [1. 1. 1. 0.67]
mean accuracy: 0.92
import scanpy as sc
adata = sc.datasets.pbmc3k() # 2,700 PBMCs, 10x Genomics
sc.pp.filter_genes(adata, min_cells=3)
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata, n_top_genes=2000)
adata = adata[:, adata.var.highly_variable].copy()
sc.pp.pca(adata, n_comps=50, random_state=0)
sc.pp.neighbors(adata, n_neighbors=15, random_state=0)
sc.tl.leiden(adata, resolution=1.0, flavor="igraph",
n_iterations=2, directed=False, random_state=0) # no labels supplied
print(adata.obs['leiden'].value_counts().sort_index())leiden
0 372
1 348
2 491
3 623
4 493
5 151
6 161
7 49
8 12
Name: count, dtype: int64
When Learning Wins
Prefer a learned model over hand-written rules when the decision boundary depends on many interacting features, when those interactions are unknown or unstable across contexts, and when you have enough labeled examples to estimate them reliably. Classical statistics still wins when the goal is a calibrated effect size or a hypothesis test on a few pre-specified variables. A logistic regression with three covariates is more interpretable and better-powered there than a gradient-boosted forest. Reach for ML when out-of-sample prediction matters more than a closed-form explanation and the signal is spread across thousands of correlated features such as expressed genes or k-mer frequency counts.
Bias-Variance Intuition
Every model trades bias against variance. A high-bias model such as linear logistic regression is too rigid to fit the true boundary and underfits, while a high-variance model such as a deep unregularized tree memorizes noise in the training set and fails to generalize. Test error is minimized between these extremes, and most of the tuning in this course, regularization strength, tree depth, and embedding dimension, is a search for that balance. Watching training error keep dropping while validation error rises is the practical signal that you have moved too far toward variance.
Try it yourself: rerun the variant classifier with RandomForestClassifier(n_estimators=1, max_depth=1), then with the full 200-tree forest above, and compare the cross_val_score means. The shallow single tree is high-bias and underfits; the full forest lowers bias while averaging many trees controls variance. That is exactly the trade-off you will tune for the rest of the course.