Machine Learning Tutorial 0/98 lessons ~6 min read Lesson 77
Q-Learning
Q-learning learns action values Q(s,a) from experience without a model of the environment.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Q-learning learns action values Q(s,a) from experience without a model of the environment. Updates push estimates toward observed reward plus discounted best next action.
Understanding the topic
Exploration epsilon-greedy balances trying new actions vs exploiting known good ones.
Tabular limits Works for small state spaces; large problems need function approximation.
- Exploration — epsilon-greedy balances trying new actions vs exploiting known good ones.
- Tabular limits — Works for small state spaces; large problems need function approximation.
Step-by-step explanation
- Exploration — epsilon-greedy balances trying new actions vs exploiting known good ones.
- Tabular limits — Works for small state spaces; large problems need function approximation.
Informative example
Python starter:
python
import numpy as np# Tabular Q-learning on 5-state corridor (state 4 = goal +10)rng = np.random.default_rng(42)Q = np.zeros((5, 2)) # 0=left, 1=rightalpha, gamma, eps = 0.2, 0.9, 0.2for _ in range(300):s = 0while s != 4:a = int(rng.integers(2)) if rng.random() < eps else int(np.argmax(Q[s]))ns = max(0, s - 1) if a == 0 else min(4, s + 1)r = 10.0 if ns == 4 else -0.1Q[s, a] += alpha * (r + gamma * np.max(Q[ns]) - Q[s, a])s = nsprint(f"Optimal policy: {Q[:4].argmax(axis=1).tolist()} | Q(0, right)={Q[0, 1]:.2f}")
Output
Optimal policy: [1, 1, 1, 1] | Q(0, right)=7.02
Execution workflow
1Q-Learning — workflow
1 / 2Exploration
epsilon-greedy balances trying new actions vs exploiting known good ones.
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:
- Implement epsilon-greedy on a grid world
- Log episode return over training
Summary
Q-Learning — Model-free action-value updates.
Ready to mark this lesson complete?Track your journey across the entire course.