Nexus Learn

Practical 6B: Time Series: Forecasting Tomorrow's Electricity Use

Lab Practical CS-511 Artificial Intelligence 8 min read Updated 8 Nov 2026
On this page
  1. 1. Objectives
  2. 2. Word Bank
  3. 3. Background
  4. 4. Time Plan (50 minutes)
  5. Part A - Guided Warm-up (type, run, observe)
  6. Part B - Predict the Output
  7. Part C - Bug Hunt
  8. Part D - Graded Tasks (10 marks)
  9. Part E - Post-Lab Questions (finish as homework if time runs out)
  10. Marking Rubric (applied to Part D)
  11. Submission

1. Objectives

  1. Explain a time series and turn it into a supervised table with lag features.
  2. Split time-series data by time (past for training, future for testing) - never shuffle.
  3. Train MLPRegressor on lag features and compare it with naive baselines using MAE.
  4. Forecast the next 7 days step by step (recursive forecasting).

2. Word Bank

Word

Simple meaning

Real-world example

Time series

Measurements taken in time order

Units used each day for two years

Lag feature

A past value used as an input

Units 1 day ago, 2 days ago, ... 7 days ago

Forecast

A prediction of a future value

Tomorrow's units

Baseline

A very simple forecast every model must beat

'Tomorrow = today'

Data leakage

Information from the future sneaks into training

Shuffling days before splitting

Recursive forecast

Use each prediction as an input for the next one

Day 1 forecast helps forecast day 2

alpha

A penalty that keeps the network's weights small, so it overfits less

alpha=1.0

3. Background

From a table to a time series

In P04b each day was predicted from that day's weather. Often we only have the PAST of the same quantity: electricity units, wheat prices in the Burewala grain market, dengue cases per week. The outline topic 'NN: Time Series Modelling' predicts the next value from previous values.

Lag features

We turn the series into a table: each row holds the units of the last 7 days (lags) and the answer is the next day's units. We also add one known fact about the next day: is it a weekend (people are at home)? Then any regression model from P04 can be used.

The golden rule

Train on the PAST, test on the FUTURE. household_series.csv has 730 days (1 July 2024 to 30 June 2026); we keep the last 120 days for testing. Shuffling the days would let the model peek at the future.

Baselines

Before trusting a network, compare it with simple rules: 'tomorrow = today' (naive) and 'tomorrow = the same day last week'. A model that cannot beat them is useless.

Data files for this lab

household_series.csv

date,units

2024-07-01,25.1

2024-07-02,24.1

2024-07-03,25.2

2024-07-04,26.5

2024-07-05,27.1

2024-07-06,27.7

2024-07-07,24.7

2024-07-08,24.8

2024-07-09,24.4

2024-07-10,25.2

... (730 data rows in total - copy the full file from the lab folder)

4. Time Plan (50 minutes)

Minutes

Activity

Part

0-5

Background: time series, lags and the golden rule

1-3

5-10

Guided warm-up: plot two years; build the lag table

Part A

10-13

Predict the output

Part B

13-17

Bug hunt

Part C

17-47

Graded tasks 1-3

Part D

47-50

Wrap-up; post-lab questions set as homework

Part E

Part A - Guided Warm-up (type, run, observe)

A1. Two years of electricity use

Look for the yearly pattern (AC in summer) and the small weekly ups and downs.

import pandas as pd

import matplotlib.pyplot as plt

df = pd.read_csv("household_series.csv", parse_dates=["date"])

print(df.shape, "| from", df["date"].min().date(), "to", df["date"].max().date())

monthly = df.groupby(df["date"].dt.month)["units"].mean().round(1)

print("average units per day by month:", monthly.to_dict())

plt.figure(figsize=(7, 2.8)); plt.plot(df["date"], df["units"], lw=0.8)

plt.ylabel("Units per day"); plt.title("One household, two years"); plt.grid(alpha=0.3)

plt.tight_layout(); plt.savefig("series.png", dpi=100)

print("Saved series.png")

Output

(730, 2) | from 2024-07-01 to 2026-06-30

average units per day by month: {1: 4.5, 2: 6.1, 3: 11.6, 4: 15.3, 5: 20.8, 6: 25.0, 7: 24.2, 8: 22.4, 9: 18.3, 10: 13.6, 11: 8.2, 12: 6.0}

Saved series.png

A2. Turn the series into a table with lag features

Row 1 uses days 1-7 to predict day 8, row 2 uses days 2-8 to predict day 9, and so on. The last column says whether the day to predict is a weekend.

import numpy as np, pandas as pd

df = pd.read_csv("household_series.csv", parse_dates=["date"])

s = df["units"].to_numpy()

weekend = (df["date"].dt.dayofweek >= 5).astype(int).to_numpy()

K = 7 # use the last 7 days

X = np.column_stack([s[i:len(s) - K + i] for i in range(K)] + [weekend[K:]])

y = s[K:] # the next day

split = len(y) - 120 # last 120 days = test (the future)

X_tr, X_te, y_tr, y_te = X[:split], X[split:], y[:split], y[split:]

cols = [f"lag{K - i}" for i in range(K)] + ["weekend"]

print(pd.DataFrame(X[:3], columns=cols).to_string(index=False))

print("targets y:", y[:3])

print("training rows:", len(y_tr), "| test rows:", len(y_te))

Output

lag7 lag6 lag5 lag4 lag3 lag2 lag1 weekend

25.1 24.1 25.2 26.5 27.1 27.7 24.7 0.0

24.1 25.2 26.5 27.1 27.7 24.7 24.8 0.0

25.2 26.5 27.1 27.7 24.7 24.8 24.4 0.0

targets y: [24.8 24.4 25.2]

training rows: 603 | test rows: 120

Part B - Predict the Output

Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.

B1.

import numpy as np

s = np.array([10, 11, 12, 13, 14])

K = 2

X = np.column_stack([s[i:len(s) - K + i] for i in range(K)]); y = s[K:]

print(X.tolist(), y.tolist())

My prediction

B2.

import numpy as np

actual = np.array([20, 22, 21]); today = np.array([19, 20, 22])

print(np.abs(actual - today).mean())

My prediction

B3.

import pandas as pd

d = pd.Timestamp("2026-10-17")

print(d.dayofweek, d.day_name(), d.dayofweek >= 5)

My prediction

Part C - Bug Hunt

Each program has ONE bug. Some crash with an error message; others run but give a wrong answer (silent logic error - the dangerous kind). Find the bug, explain it and write the fix.

C1.

import numpy as np, pandas as pd

df = pd.read_csv("household_series.csv", parse_dates=["date"])

s = df["units"].to_numpy()

weekend = (df["date"].dt.dayofweek >= 5).astype(int).to_numpy()

K = 7 # use the last 7 days

X = np.column_stack([s[i:len(s) - K + i] for i in range(K)] + [weekend[K:]])

y = s[K:] # the next day

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_absolute_error

X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=120, random_state=42) # random split

m = LinearRegression().fit(X_tr, y_tr)

print("test MAE:", round(mean_absolute_error(y_te, m.predict(X_te)), 2))

It runs and prints

test MAE: 1.29

Bug, why it happens, and the fixed line(s)

C2.

import pandas as pd

df = pd.read_csv("household_series.csv") # dates are read as text

weekend = (df["date"].dt.dayofweek >= 5)

Bug, why it happens, and the fixed line(s)

C3.

import numpy as np, pandas as pd

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_absolute_error

s = pd.read_csv("household_series.csv")["units"].to_numpy()

K = 7

X = np.column_stack([s[i + 1:len(s) - K + i + 1] for i in range(K)]) # 7 lags

y = s[K:]

m = LinearRegression().fit(X[:-120], y[:-120])

print("test MAE:", round(mean_absolute_error(y[-120:], m.predict(X[-120:])), 3))

It runs and prints

test MAE: 0.0

Bug, why it happens, and the fixed line(s)

Part D - Graded Tasks (10 marks)

Task

Level

Title

Marks

T1

Easy

Baselines first

3

T2

Medium

Can a network beat the baseline?

3

T3

Challenge

Plot the test period and forecast the next 7 days

4

Total

10

Task 1 (Easy, 3 marks): Baselines first

Using the A2 table and the time split, compute the test MAE of two baselines: naive ('tomorrow = today', the lag1 column) and seasonal-naive ('tomorrow = the same day last week', the lag7 column). Which baseline is better and why?

Sample run (your output must match):

Output

naive (tomorrow = today) MAE: 1.76

same day last week MAE: 2.37

Your code (or attach the file)

Task 2 (Medium, 3 marks): Can a network beat the baseline?

Train LinearRegression and make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(8,), solver='lbfgs', alpha=1.0, max_iter=5000, random_state=1)) on the training rows. Print the test MAE of the naive baseline, the linear model and the network, and the improvement of the network over naive in percent.

Sample run (your output must match):

Output

naive 1.76 | linear 1.46 | MLP (8,) 1.47 units/day

MLP is 16% better than naive

Your code (or attach the file)

Task 3 (Challenge, 4 marks): Plot the test period and forecast the next 7 days

Train the (8,) network on ALL rows. (a) Plot actual and predicted units for the last 60 test days (save task3.png). (b) Forecast 1-7 July 2026 recursively: start from the last 7 real days, predict one day, add it to the history, repeat. Print each date with its weekday and forecast.

Sample run (your output must match):

Output

2026-07-01 Wed 26.2 units

2026-07-02 Thu 25.9 units

2026-07-03 Fri 25.0 units

2026-07-04 Sat 27.3 units

2026-07-05 Sun 28.3 units

2026-07-06 Mon 26.5 units

2026-07-07 Tue 25.9 units

Saved task3.png

Your code (or attach the file)

Part E - Post-Lab Questions (finish as homework if time runs out)

E1. What are lag features? Write the first row of the lag table for K = 3 and the series 5, 6, 8, 9.

Your answer

E2. Why must a time series be split by time and not shuffled?

Your answer

E3. Why compare with a naive baseline? What did you find?

Your answer

Marking Rubric (applied to Part D)

Criterion

Full

Partial

None

Code runs without errors

Runs fully

Small fixable error

Does not run

Correct lag table and time split (no leakage)

All correct

One slip

Several wrong

MAEs, plot and 7-day forecast

All present

Some missing

None

Explanations (baselines, leakage, model choice)

Clear and correct

Partly correct

Missing

Submission

  • Make a folder RollNo_P06b with task1.py, task2.py, task3.py (or P06b.ipynb), household_series.csv and task3.png.
  • Upload the zipped folder to the LMS / Google Classroom by the end of the day.
  • Copy Task 2 and Task 3 code and output into your Lab Book (Practical 06b page).
  • Use random_state exactly as given so your numbers match the sample runs.

← All CS-511 material