Introduction to Machine Learning
Machine Learning teaches computers to learn patterns from data without being explicitly programmed for each task. It is the engine behind recommendation systems, image recognition, natural language processing, and predictive analytics.
The three types of ML
Supervised Learning
- Labelled training data
- Learns input → output mapping
- Regression, classification
Unsupervised Learning
- No labels — find structure
- Clustering, dimensionality reduction
- k-Means, PCA, autoencoders
Reinforcement Learning
- Agent takes actions in environment
- Learns via reward signals
- Game playing, robotics, LLM RLHF
The ML pipeline
1. Define the problem → What are we predicting? Success metric?
2. Collect data → Gather labelled examples
3. Explore & clean data → EDA, handle missing values, outliers
4. Feature engineering → Transform raw data into useful signals
5. Choose a model → Linear, tree, neural network, etc.
6. Train the model → Fit on training data
7. Evaluate → Measure on held-out test data
8. Tune hyperparameters → Grid search / Bayesian optimisation
9. Deploy & monitor → Serve predictions; watch for drift
Your first ML model with scikit-learn
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
# 1. Load data
data = load_iris()
X, y = data.data, data.target # 150 samples, 4 features
print(f"Features: {data.feature_names}")
print(f"Classes: {data.target_names}")
# 2. Split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# 3. Scale
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train) # fit on train only
X_test = scaler.transform(X_test) # apply same transform
# 4. Train
model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
# 5. Evaluate
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred):.2%}")
print(classification_report(y_test, y_pred, target_names=data.target_names))
Key terminology
| Term | Meaning |
|---|---|
Feature (X) | Input variable used for prediction |
Label / Target (y) | Output the model tries to predict |
Training set | Data the model learns from |
Validation set | Used to tune hyperparameters during development |
Test set | Held-out data for final evaluation — never touched during training |
Hyperparameter | Model setting chosen before training (e.g. n_neighbors, learning rate) |
Overfitting | Model memorises training data, poor generalisation |
Underfitting | Model too simple, misses patterns even in training data |
Bias | Error from wrong assumptions — high bias → underfitting |
Variance | Sensitivity to training data noise — high variance → overfitting |
The bias-variance tradeoff
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import numpy as np
# Generate noisy sine curve
rng = np.random.default_rng(0)
X = np.sort(rng.uniform(0, 1, 40))
y = np.sin(2 * np.pi * X) + rng.normal(0, 0.3, 40)
X = X.reshape(-1, 1)
# Underfit: degree 1 (too simple)
# Good fit: degree 4
# Overfit: degree 15 (too complex)
for degree in [1, 4, 15]:
pipe = Pipeline([
("poly", PolynomialFeatures(degree)),
("reg", LinearRegression()),
])
pipe.fit(X, y)
train_score = pipe.score(X, y)
print(f"Degree {degree:2d} | train R² = {train_score:.3f}")
Environment setup
# Create isolated environment
python -m venv ml-env
source ml-env/bin/activate # Windows: ml-env\Scripts\activate
# Core ML stack
pip install numpy pandas scikit-learn matplotlib seaborn jupyter
# Deep learning (choose one)
pip install torch torchvision # PyTorch (recommended)
pip install tensorflow # TensorFlow / Keras
# Extras
pip install xgboost lightgbm # gradient boosting
pip install shap # model explainability
Which library? Start with scikit-learn for classical ML (regression, trees, SVM, clustering). Move to PyTorch when you need neural networks. Use XGBoost / LightGBM for tabular competitions — they consistently outperform everything else on structured data.