Machine Learning Tutorial 0/98 lessons ~6 min read Lesson 78
SARSA
SARSA — On-policy TD control variant.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
SARSA — On-policy TD control variant. This lesson pairs the idea with a minimal Python/sklearn workflow you can extend in a notebook.
Understanding the topic
Concept On-policy TD control variant.
Workflow Load data → preprocess → fit or apply technique → measure on hold-out data.
In practice Start with a small public dataset before jumping to proprietary production data.
- Concept — On-policy TD control variant.
- Workflow — Load data → preprocess → fit or apply technique → measure on hold-out data.
- In practice — Start with a small public dataset before jumping to proprietary production data.
Step-by-step explanation
- Concept — On-policy TD control variant.
- Workflow — Load data → preprocess → fit or apply technique → measure on hold-out data.
- In practice — Start with a small public dataset before jumping to proprietary production data.
Informative example
Python starter:
python
import numpy as np# On-policy SARSA on 5-state corridor (state 4 = goal +10)rng = np.random.default_rng(42)Q = np.zeros((5, 2))alpha, gamma, eps = 0.2, 0.9, 0.1pick = lambda s: int(rng.integers(2)) if rng.random() < eps else int(np.argmax(Q[s]))for _ in range(300):s, a = 0, pick(0)while s != 4:ns = max(0, s - 1) if a == 0 else min(4, s + 1)r = 10.0 if ns == 4 else -0.1na = pick(ns)Q[s, a] += alpha * (r + gamma * Q[ns, na] - Q[s, a])s, a = ns, naprint(f"Learned policy: {Q[:4].argmax(axis=1).tolist()} | Q(0, right)={Q[0, 1]:.2f}")
Output
Learned policy: [1, 1, 1, 1] | Q(0, right)=6.61
Execution workflow
1SARSA — workflow
1 / 3Concept
On-policy TD control variant.
Best practices
- Hold out a test set before hyperparameter tuning.
- Scale numeric columns for distance-based models.
- Track multiple metrics — not accuracy alone on skewed labels.
Common mistakes
- Leaking test statistics into preprocessing fit on full data.
- Training on the same rows you report as test performance.
- Chasing complex models before a simple baseline.
Hands-on exercise
Practice:
- Apply SARSA on a sample dataset
- Write down one metric that proves the technique helped
Summary
SARSA: On-policy TD control variant.
Ready to mark this lesson complete?Track your journey across the entire course.