Menu
CoddyTech

Air Quality

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 62 of 64.

A city wants a quick forecast of fine dust, PM2.5. Its street sensor logged 40 days in air.csv:

A line of daily PM2.5 over 40 days, swinging between about 16 and 61, every day above a dashed line at 15 marked WHO daily guideline

Each row holds a day's wind, temperature, humidity, traffic and pm25. One of them tracks the dust far better than the rest.

Find it, fit a line, and say how far to trust it: how much it explains, where it misses most, and when a forecast leaves the data behind.

challenge icon

Challenge

Hard

The program reads a new day's value of the best feature. Round to 2 decimals.

  1. Print the feature with the strongest r with pm25, sign ignored, and its signed r.
  2. Fit a line. Print it, its R squared, and the row it misses most with that residual.
  3. Predict for the value, and warn when it lies outside the feature's data.
  4. Draw dots and the red line from the smallest value to the largest: title Air Quality, the feature across, PM2.5 up.

For another city and the input 8:

Best feature: traffic_k (r = 0.86)
Line: pm25 = 1.11 * traffic_k + 13.67
R squared: 0.73
Largest miss: row 14 (residual 11.15)
Prediction at 8.0: 22.57
Warning: outside the data

A scatter titled Air Quality, traffic_k across and PM2.5 up, 30 dots rising to the right with a red fitted line through them

After your lines, Coddy describes your chart.

Try it yourself

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

value = float(input())
df = pd.read_csv("air.csv")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler