Skip to content

Introduction to Machine Learning

This section is a minimal, practical introduction for biologists applying ML to tabular biology datasets (e.g. sample-by-feature tables: clinical variables, gene panels, assay readouts). It is concepts + one reproducible sklearn example — not a full ML course.

For deeper study: https://www.coursera.org/specializations/machine-learning-introduction

1. Core vocabulary

  • Supervised learning: you have labels. Classification (disease vs control) or regression (predict a continuous value like age, expression level).
  • Unsupervised learning: no labels. Clustering (find subtypes), dimensionality reduction (PCA/UMAP for visualisation).
  • Features (X) vs target (y): rows = samples, columns = features; one column is the target you predict.
  • Train / validation / test split: train fits the model, validation tunes it, test estimates real-world performance once. Never tune on the test set.
  • Leakage: information from the test set (or future) leaks into training — e.g. normalising on the full dataset, selecting features using all samples. This inflates performance and fails on new data.
  • Overfitting vs underfitting: overfit = memorises training noise (train high, test low); underfit = too simple (both low). More data, simpler models, and regularisation help.
  • Cross-validation (CV): split train into k folds, rotate which fold validates. Use stratified k-fold for imbalanced classes.
  • Class imbalance: biology is often 90/10. Accuracy lies — report precision, recall, F1, ROC-AUC, PR-AUC and the confusion matrix.

2. Minimal sklearn workflow

Prerequisites: the venv + pinned requirements.txt workflow from earlier. Add:

requirements.txt
pandas==2.2.3
scikit-learn==1.6.0
Python
import pandas as pd
from sklearn.model_selection import train_test_split, StratifiedKFold, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.metrics import classification_report, confusion_matrix

# 1. Load: rows = samples, columns = features + label
df = pd.read_csv("samples.csv")
X = df.drop(columns=["label"])
y = df["label"]

# 2. Split BEFORE any fitting (stratify preserves class ratio)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# 3. Pipeline bundles scaling + model (prevents leakage)
pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))

# 4. Cross-validate on train only
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(pipe, X_train, y_train, cv=cv, scoring="f1")
print(f"CV F1: {scores.mean():.3f} +/- {scores.std():.3f}")

# 5. Fit on full train, evaluate ONCE on test
pipe.fit(X_train, y_train)
print(classification_report(y_test, pipe.predict(X_test)))
print(confusion_matrix(y_test, pipe.predict(X_test)))

Verify it worked

CV F1 prints without errors; test report shows precision/recall/F1 per class — not just accuracy. If test >> CV, suspect leakage. If both are low, start simpler (fewer features, logistic regression) before trying bigger models.

3. Reproducibility checklist (biology-specific)

  • random_state set everywhere; record scikit-learn==x.y.z, Python version, and git commit hash with results.
  • Split by biological unit (patient/donor/batch), not by row, when rows are correlated — otherwise leakage.
  • Keep batch/site info as a column; check performance per batch.
  • Save the fitted pipeline (joblib) + test IDs so anyone can reproduce the exact numbers.
  • On Azure: run the same script + requirements.txt + Dockerfile on the VM; results should match your laptop.

4. Ethics and limits

  • De-identified data only outside TREs; Trusted Research Environments (SafeHaven/DataLoch) rules override everything here.
  • Report limitations: sample size, selection bias, class imbalance, and that test performance ≠ clinical utility.
  • Do not deploy to clinical use from this tutorial — that needs validation, governance, and monitoring far beyond this scope.