1. Objectives
- Approximate a function of several inputs (temperature, humidity, weekend) with MLPRegressor.
- Build a Pipeline of StandardScaler and MLPRegressor so new data is always scaled the same way.
- Evaluate with R² and mean absolute error (MAE) and compare with a straight-line model.
- Use the model for a real decision (a monthly bill estimate) and find where it makes its biggest errors.
2. Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Feature | One input column | temp_c, humidity, weekend |
Target | The number we want to predict | units used in the day (kWh) |
MAE | Mean absolute error: average size of the mistakes, in the target's own unit | MAE 1.2 units per day |
Pipeline | A chain of steps (scale, then model) that acts like one model | make_pipeline(StandardScaler(), MLPRegressor()) |
Residual | True value minus predicted value | 9.1 - 3.9 = 5.2 units |
Data leakage | The answer sneaks into the inputs, so the score is falsely perfect | Using 'units' as a feature |
kWh (unit) | One unit on the electricity bill = 1 kilowatt-hour | A 1.5-ton AC uses about 1.5 units per hour |
3. Background
From one input to many
In P04a the network learned yield from ONE input (nitrogen). Real problems have several inputs. Electricity use in a Burewala house depends on the temperature (AC in June, heater in January), on humidity (the AC works harder on humid days) and on whether it is a weekend (everyone is at home). The rule connecting them is a curved surface, not a flat plane - a job for a multilayer network.
The data
daily_units.csv has 120 days of one household: temp_c (°C), humidity (%), weekend (1 = Saturday/Sunday) and units (kWh used that day). Units are low on mild days (about 20-26 °C) and rise on both hot and cold days.
Why a Pipeline
In P03 Bug C4 the model was tested on unscaled data. A Pipeline joins StandardScaler and the network, so fit() scales the training data and predict() scales every new day in exactly the same way.
The tariff (from P01)
In P01 you used a simple tariff: Rs 22 per unit if the month's units are 200 or less, otherwise Rs 35 per unit for all units. We use it again to turn a prediction into a bill.
Data files for this lab
daily_units.csv
day,temp_c,humidity,weekend,units D001,39,94,1,24.4 D002,16,33,1,8.8 D003,10,57,0,4.2 D004,17,33,0,6.1 D005,22,79,0,5.1 D006,38,65,0,17.7 D007,24,32,0,4.1 D008,9,28,0,8.1 D009,19,27,0,6.1 D010,30,21,0,7.6 ... (120 data rows in total - copy the full file from the lab folder) |
4. Time Plan (50 minutes)
Minutes | Activity | Part |
|---|---|---|
0-5 | Background, link to P04a, the data | 1-3 |
5-10 | Guided warm-up: look at the data; line vs network | 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. Look at the data
The correlation of temperature with units is high but humidity and weekend look weak - yet they still matter on hot days. The plot shows a U shape: high units on cold AND hot days.
import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv("daily_units.csv") print(df.shape, "| units from", df["units"].min(), "to", df["units"].max()) print(df[["temp_c", "humidity", "weekend", "units"]].corr()["units"].round(3).to_string()) plt.figure(figsize=(6, 3.2)) plt.scatter(df["temp_c"], df["units"], c=df["humidity"], cmap="viridis", s=18) plt.colorbar(label="humidity (%)") plt.xlabel("Temperature (°C)"); plt.ylabel("Units (kWh per day)") plt.title("Daily electricity units vs temperature"); plt.grid(alpha=0.3) plt.tight_layout(); plt.savefig("units.png", dpi=100) print("Saved units.png") |
Output (120, 5) | units from 3.7 to 39.5 temp_c 0.763 humidity 0.048 weekend 0.050 units 1.000 Saved units.png |
A2. A flat plane vs a network
Same split for both models. The network is inside a Pipeline with StandardScaler.
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.neural_network import MLPRegressor df = pd.read_csv("daily_units.csv") X = df[["temp_c", "humidity", "weekend"]].to_numpy(dtype=float); y = df["units"].to_numpy() X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42) line = LinearRegression().fit(X_tr, y_tr) net = make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(5,), solver="lbfgs", max_iter=5000, random_state=1)).fit(X_tr, y_tr) print("test R2 line:", round(line.score(X_te, y_te), 3), "| network (5,):", round(net.score(X_te, y_te), 3)) |
Output test R2 line: 0.5 | network (5,): 0.957 |
Part B - Predict the Output
Write your prediction BEFORE running the code. Then run it and correct yourself in a different colour.
B1.
from sklearn.metrics import mean_absolute_error print(mean_absolute_error([10, 20], [12, 17])) |
My prediction |
B2.
from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.neural_network import MLPRegressor pipe = make_pipeline(StandardScaler(), MLPRegressor()) print(list(pipe.named_steps)) |
My prediction |
B3.
units_per_day = 8 month_units = units_per_day * 30 rate = 22 if month_units <= 200 else 35 print(month_units, rate, month_units * rate) |
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 pandas as pd from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.neural_network import MLPRegressor df = pd.read_csv("daily_units.csv") y = df["units"].to_numpy() X = df[["temp_c", "humidity", "weekend"]].to_numpy(dtype=float) net = make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(5,), solver="lbfgs", max_iter=5000, random_state=1)).fit(X, y) print(net.predict([42, 60, 0])) # a hot weekday |
Bug, why it happens, and the fixed line(s) |
C2.
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.neural_network import MLPRegressor df = pd.read_csv("daily_units.csv") y = df["units"].to_numpy() X = df[["temp_c", "humidity", "weekend", "units"]].to_numpy(dtype=float) X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=42) net = make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(5,), solver="lbfgs", max_iter=5000, random_state=1)).fit(X_tr, y_tr) print("test R2:", round(net.score(X_te, y_te), 3)) |
It runs and prints test R2: 1.0 |
Bug, why it happens, and the fixed line(s) |
C3.
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.neural_network import MLPRegressor df = pd.read_csv("daily_units.csv") y = df["units"].to_numpy() X = df[["temp_c", "humidity", "weekend"]].to_numpy(dtype=float) net = make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(5,), solver="lbfgs", max_iter=5000, random_state=1)).fit(X, y) hot_day = [[60, 42, 0]] # 42 °C, 60% humidity, weekday print("predicted units:", net.predict(hot_day).round(1)) |
It runs and prints predicted units: [74.7] |
Bug, why it happens, and the fixed line(s) |
Part D - Graded Tasks (10 marks)
Task | Level | Title | Marks |
|---|---|---|---|
T1 | Easy | Measure the network with R² and MAE | 3 |
T2 | Medium | Forecast three days and estimate the bill | 3 |
T3 | Challenge | Choose the network and find its worst day | 4 |
Total | 10 |
Task 1 (Easy, 3 marks): Measure the network with R² and MAE
Use the A2 split (test_size=0.25, random_state=42). Train LinearRegression and the pipeline make_pipeline(StandardScaler(), MLPRegressor(hidden_layer_sizes=(20,), solver='lbfgs', max_iter=5000, random_state=1)). For both print the test R² and the test MAE (units per day). Say in one line how many units per day the network's typical mistake is.
Sample run (your output must match):
Output straight line test R2 0.500 MAE 4.59 units/day network (20,) test R2 0.956 MAE 1.19 units/day |
Your code (or attach the file) |
Task 2 (Medium, 3 marks): Forecast three days and estimate the bill
Train the (20,) pipeline on ALL 120 days. Predict the units for three forecast days: (42 °C, 60%, weekday), (18 °C, 50%, weekend) and (34 °C, 85%, weekday). Then assume a whole month (30 days) is like the first day: print the month's units and the bill with the P01 tariff (Rs 22 per unit up to 200 units, otherwise Rs 35 per unit).
Sample run (your output must match):
Output 42 °C, 60% humidity, weekday -> 28.0 units 18 °C, 50% humidity, weekend -> 9.3 units 34 °C, 85% humidity, weekday -> 13.0 units June-like month: 839 units x Rs 35 = Rs 29,365 |
Your code (or attach the file) |
Task 3 (Challenge, 4 marks): Choose the network and find its worst day
With the A2 split, compare hidden_layer_sizes (5,), (20,) and (20, 10): print the test R² and MAE of each. For the (20,) network plot predicted against actual units for the test days with the line y = x (save task3.png), and print the test day with the largest error (its temperature, humidity, true and predicted units). Explain in two lines why that day is hard and why the bigger (20, 10) network is not better.
Sample run (your output must match):
Output (5,) test R2 0.957 MAE 1.30 (20,) test R2 0.956 MAE 1.19 (20, 10) test R2 0.806 MAE 2.48 worst day: 8 °C, 54% -> true 9.1 units, predicted 3.9 Saved task3.png |
Your code (or attach the file) |
Part E - Post-Lab Questions (finish as homework if time runs out)
E1. Why do we put StandardScaler and MLPRegressor into one Pipeline?
Your answer |
E2. What is data leakage? How did it show up in Bug C2?
Your answer |
E3. MAE was about 1.2 units per day. What does that mean for a monthly bill?
Your answer |
Marking Rubric (applied to Part D)
Criterion | Full | Partial | None |
|---|---|---|---|
Code runs without errors | Runs fully | Small fixable error | Does not run |
Pipeline, split and column order correct | All correct | One slip | Several wrong |
R², MAE, predictions and plot | All present | Some missing | None |
Explanations (bill, worst day, network size) | Clear and correct | Partly correct | Missing |
Submission
- Make a folder RollNo_P04b with task1.py, task2.py, task3.py (or P04b.ipynb), daily_units.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 04b page).
- Use random_state exactly as given so your numbers match the sample runs.