0Pricing
Learn AI with Python · Lesson

Implementing Q-Table in Python

A simple example of Q-learning.

1

Implementing Q-Learning in Python

In this lesson, we will implement a simple Q-Learning algorithm in Python. The agent will learn to navigate a grid world to maximize its rewards.

Implementing Q-Table in Python — illustration 1

2

Defining the Environment

We define a simple 4×4 grid world where the agent starts at the top-left corner and must reach the bottom-right corner to receive a reward.

import numpy as np

# Define the environment
num_states = 16  # 4x4 grid
num_actions = 4  # Up, Down, Left, Right

# Define rewards (-1 for each step, +10 for the goal)
rewards = np.zeros(num_states)
rewards[-1] = 10

All lessons in this course

  1. Basic Concepts in Reinforcement Learning
  2. Q-Table Concept
  3. Implementing Q-Table in Python
  4. Deep Q-Learning
  5. Exploring OpenAI Gym
← Back to Learn AI with Python