Introduction to Machine Learning
Types of ML, the ML workflow, and when to use ML
Skills taught: ml.basics, ml.workflow, ml.types
What is Machine Learning?
Machine Learning is the field of building systems that learn from data rather than being explicitly programmed. You supply labelled examples; the algorithm discovers the mapping from inputs to outputs and can apply it to new, unseen data.
The Three Types of Machine Learning
- Supervised Learning
- Training on labelled examples where both input <code>X</code> and desired output <code>y</code> are known. Used for classification (discrete labels) and regression (continuous values). Examples: spam detection, house price prediction.
- Unsupervised Learning
- Finding hidden structure in unlabelled data. The algorithm groups, reduces, or generates data without being told the 'right answer'. Examples: customer segmentation, anomaly detection, dimensionality reduction.
- Reinforcement Learning
- An agent learns by interacting with an environment and receiving reward signals. It maximises cumulative reward over time. Examples: game-playing AI, robotics, recommendation systems.
Supervised vs Unsupervised Learning
- Supervised: Learns from labelled input-output pairs to make predictions
- Unsupervised: Discovers hidden patterns in data without labels
The ML Workflow
- Collect & clean data — quality data beats fancy algorithms
- Exploratory data analysis (EDA) — understand distributions and relationships
- Feature engineering — create informative inputs from raw data
- Choose and train a model — fit parameters to minimise loss
- Evaluate on held-out data — measure real-world performance
- Tune hyperparameters — systematically improve performance
- Deploy and monitor — serve predictions and watch for drift
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
# 1. Load data
df = pd.read_csv('dataset.csv')
X = df.drop('target', axis=1)
y = df['target']
# 2. Split: 80% train, 20% test — always before any preprocessing
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# 3. Scale (fit on train ONLY to prevent data leakage)
scaler = StandardScaler()
X_train_sc = scaler.fit_transform(X_train)
X_test_sc = scaler.transform(X_test)
# 4. Train
model = LogisticRegression(max_iter=1000)
model.fit(X_train_sc, y_train)
# 5. Evaluate
preds = model.predict(X_test_sc)
print(classification_report(y_test, preds))
Tip: Split your data before any preprocessing. Fitting a scaler on the full dataset leaks test-set information into training, making your results look better than they really are.
The 80/20 rule is a common starting split. For small datasets (< 1 000 rows) use cross-validation instead of a single split to get a reliable performance estimate.
Warning: More data almost always beats a better algorithm. Before tuning models, check whether you can collect more or higher-quality data.
When NOT to Use Machine Learning
- The problem is solvable with simple rules or a lookup table
- You have fewer than a few hundred labelled examples
- The model's decision must be fully explainable to regulators
- The cost of a wrong prediction is catastrophic and unrecoverable
Frequently Asked Questions
Do I need a PhD to do ML?
No. With Python, scikit-learn, and a good dataset you can build useful models. Deep learning at scale (training large models from scratch) requires more specialised knowledge, but applied ML is accessible to any programmer.
How much data do I need?
It depends on the complexity of the problem. Simple classifiers can work with hundreds of examples; image models often need tens of thousands. As a rule of thumb, you need at least 10× more samples than features.
What is the difference between AI and ML?
AI is the broad field of making machines behave intelligently. ML is a subset that focuses on systems that learn from data. Deep learning is a further subset of ML using neural networks with many layers.