Upload folder using huggingface_hub
Browse filesFiles changed (11)
- notebooks/01_eda.ipynbAdded
- notebooks/02_preprocessing.ipynbAdded+647
- notebooks/03_feature_engineering.ipynbAdded
- notebooks/04_pattern_mining.ipynbAdded
- notebooks/05_modeling.ipynbAdded
- notebooks/models/lgbm_spike_t1.joblibAdded+3
- notebooks/models/lgbm_spike_t2.joblibAdded+3
- notebooks/models/lgbm_spike_t3.joblibAdded+3
- notebooks/models/xgb_spike_t1.joblibAdded+3
- notebooks/models/xgb_spike_t2.joblibAdded+3
- notebooks/models/xgb_spike_t3.joblibAdded+3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647{ "cells": [ { "cell_type": "markdown", "id": "e90de4ca", "metadata": {}, "source": [ "# 02 - Preprocessing" ] }, { "cell_type": "markdown", "id": "3cb9a2cf", "metadata": {}, "source": [ "## 1. Load & build datetime" ] }, { "cell_type": "code", "execution_count": 2, "id": "ba60a85a", "metadata": { "execution": { "iopub.execute_input": "2026-09-11T20:52:51.179569Z", "iopub.status.busy": "2026-09-11T20:52:51.179319Z", "iopub.status.idle": "2026-09-11T20:52:51.799332Z", "shell.execute_reply": "2026-09-11T20:52:51.798391Z" } }, "outputs": [ { "data": { "application/vnd.microsoft.datawrangler.viewer.v0+json": { "columns": [ { "name": "index", "rawType": "int64", "type": "integer" }, { "name": "No", "rawType": "int64", "type": "integer" }, { "name": "year", "rawType": "int64", "type": "integer" }, { "name": "month", "rawType": "int64", "type": "integer" }, { "name": "day", "rawType": "int64", "type": "integer" }, { "name": "hour", "rawType": "int64", "type": "integer" }, { "name": "PM2.5", "rawType": "float64", "type": "float" }, { "name": "PM10", "rawType": "float64", "type": "float" }, { "name": "SO2", "rawType": "float64", "type": "float" }, { "name": "NO2", "rawType": "float64", "type": "float" }, { "name": "CO", "rawType": "float64", "type": "float" }, { "name": "O3", "rawType": "float64", "type": "float" }, { "name": "TEMP", "rawType": "float64", "type": "float" }, { "name": "PRES", "rawType": "float64", "type": "float" }, { "name": "DEWP", "rawType": "float64", "type": "float" }, { "name": "RAIN", "rawType": "float64", "type": "float" }, { "name": "wd", "rawType": "str", "type": "string" }, { "name": "WSPM", "rawType": "float64", "type": "float" }, { "name": "station", "rawType": "str", "type": "string" }, { "name": "datetime", "rawType": "datetime64[us]", "type": "datetime" } ], "ref": "78dc2aea-ee9d-4921-85e4-927fd8146a81", "rows": [ [ "0", "173641", "2022", "8", "4", "0", "7.4", "11.6", "0.4", "2.0", "110.0", "28.0", "26.3", "1003.8", "25.0", "0.0", "SE", "3.68", "Barisal", "2022-08-04 00:00:00" ], [ "1", "173642", "2022", "8", "4", "1", "5.0", "8.1", "0.4", "2.0", "108.0", "28.0", "26.8", "1004.8", "24.8", "0.1", "SE", "3.7", "Barisal", "2022-08-04 01:00:00" ], [ "2", "173643", "2022", "8", "4", "2", "4.7", "7.5", "0.4", "2.1", "105.0", "28.0", "28.2", "1004.5", "25.0", "0.0", "SE", "4.56", "Barisal", "2022-08-04 02:00:00" ], [ "3", "173644", "2022", "8", "4", "3", "4.9", "7.7", "0.4", "2.1", "101.0", "29.0", "28.9", "1004.8", "25.1", "0.0", "SE", "4.2", "Barisal", "2022-08-04 03:00:00" ], [ "4", "173645", "2022", "8", "4", "4", "5.2", "8.1", "0.4", "1.7", "99.0", "32.0", "30.1", "1004.6", "25.4", "0.2", "SE", "4.18", "Barisal", "2022-08-04 04:00:00" ] ], "shape": { "columns": 19, "rows": 5 } }, "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>No</th>\n", " <th>year</th>\n", " <th>month</th>\n", " <th>day</th>\n", " <th>hour</th>\n", " <th>PM2.5</th>\n", " <th>PM10</th>\n", " <th>SO2</th>\n", " <th>NO2</th>\n", " <th>CO</th>\n", " <th>O3</th>\n", " <th>TEMP</th>\n", " <th>PRES</th>\n", " <th>DEWP</th>\n", " <th>RAIN</th>\n", " <th>wd</th>\n", " <th>WSPM</th>\n", " <th>station</th>\n", " <th>datetime</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>173641</td>\n", " <td>2022</td>\n", " <td>8</td>\n", " <td>4</td>\n", " <td>0</td>\n", " <td>7.4</td>\n", " <td>11.6</td>\n", " <td>0.4</td>\n", " <td>2.0</td>\n", " <td>110.0</td>\n", " <td>28.0</td>\n", " <td>26.3</td>\n", " <td>1003.8</td>\n", " <td>25.0</td>\n", " <td>0.0</td>\n", " <td>SE</td>\n", " <td>3.68</td>\n", " <td>Barisal</td>\n", " <td>2022-08-04 00:00:00</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>173642</td>\n", " <td>2022</td>\n", " <td>8</td>\n", " <td>4</td>\n", " <td>1</td>\n", " <td>5.0</td>\n", " <td>8.1</td>\n", " <td>0.4</td>\n", " <td>2.0</td>\n", " <td>108.0</td>\n", " <td>28.0</td>\n", " <td>26.8</td>\n", " <td>1004.8</td>\n", " <td>24.8</td>\n", " <td>0.1</td>\n", " <td>SE</td>\n", " <td>3.70</td>\n", " <td>Barisal</td>\n", " <td>2022-08-04 01:00:00</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>173643</td>\n", " <td>2022</td>\n", " <td>8</td>\n", " <td>4</td>\n", " <td>2</td>\n", " <td>4.7</td>\n", " <td>7.5</td>\n", " <td>0.4</td>\n", " <td>2.1</td>\n", " <td>105.0</td>\n", " <td>28.0</td>\n", " <td>28.2</td>\n", " <td>1004.5</td>\n", " <td>25.0</td>\n", " <td>0.0</td>\n", " <td>SE</td>\n", " <td>4.56</td>\n", " <td>Barisal</td>\n", " <td>2022-08-04 02:00:00</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>173644</td>\n", " <td>2022</td>\n", " <td>8</td>\n", " <td>4</td>\n", " <td>3</td>\n", " <td>4.9</td>\n", " <td>7.7</td>\n", " <td>0.4</td>\n", " <td>2.1</td>\n", " <td>101.0</td>\n", " <td>29.0</td>\n", " <td>28.9</td>\n", " <td>1004.8</td>\n", " <td>25.1</td>\n", " <td>0.0</td>\n", " <td>SE</td>\n", " <td>4.20</td>\n", " <td>Barisal</td>\n", " <td>2022-08-04 03:00:00</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>173645</td>\n", " <td>2022</td>\n", " <td>8</td>\n", " <td>4</td>\n", " <td>4</td>\n", " <td>5.2</td>\n", " <td>8.1</td>\n", " <td>0.4</td>\n", " <td>1.7</td>\n", " <td>99.0</td>\n", " <td>32.0</td>\n", " <td>30.1</td>\n", " <td>1004.6</td>\n", " <td>25.4</td>\n", " <td>0.2</td>\n", " <td>SE</td>\n", " <td>4.18</td>\n", " <td>Barisal</td>\n", " <td>2022-08-04 04:00:00</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " No year month day hour PM2.5 PM10 SO2 NO2 CO O3 TEMP \\\n", "0 173641 2022 8 4 0 7.4 11.6 0.4 2.0 110.0 28.0 26.3 \n", "1 173642 2022 8 4 1 5.0 8.1 0.4 2.0 108.0 28.0 26.8 \n", "2 173643 2022 8 4 2 4.7 7.5 0.4 2.1 105.0 28.0 28.2 \n", "3 173644 2022 8 4 3 4.9 7.7 0.4 2.1 101.0 29.0 28.9 \n", "4 173645 2022 8 4 4 5.2 8.1 0.4 1.7 99.0 32.0 30.1 \n", "\n", " PRES DEWP RAIN wd WSPM station datetime \n", "0 1003.8 25.0 0.0 SE 3.68 Barisal 2022-08-04 00:00:00 \n", "1 1004.8 24.8 0.1 SE 3.70 Barisal 2022-08-04 01:00:00 \n", "2 1004.5 25.0 0.0 SE 4.56 Barisal 2022-08-04 02:00:00 \n", "3 1004.8 25.1 0.0 SE 4.20 Barisal 2022-08-04 03:00:00 \n", "4 1004.6 25.4 0.2 SE 4.18 Barisal 2022-08-04 04:00:00 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "DATA_PATH = \"Bangladesh_Multi_Site_Air_Quality.csv\"\n", "\n", "df = pd.read_csv(\"../Bangladesh_Multi_Site_Air_Quality.csv\")\n", "df[\"datetime\"] = pd.to_datetime(df[[\"year\", \"month\", \"day\", \"hour\"]])\n", "df = df.sort_values([\"station\", \"datetime\"]).reset_index(drop=True)\n", "df.head()" ] }, { "cell_type": "markdown", "id": "f69d7a28", "metadata": {}, "source": [ "## 2. Check for missing hourly timestamps per station\\nEssential before building lag/rolling features - gaps would silently corrupt them." ] }, { "cell_type": "code", "execution_count": 3, "id": "d0f63a33", "metadata": { "execution": { "iopub.execute_input": "2026-09-11T20:52:51.801689Z", "iopub.status.busy": "2026-09-11T20:52:51.800890Z", "iopub.status.idle": "2026-09-11T20:52:51.824081Z", "shell.execute_reply": "2026-09-11T20:52:51.823311Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Checking for timestamp gaps per station...\n", " Barisal: 0 missing hourly timestamps out of 34728\n", " Chittagong: 0 missing hourly timestamps out of 34728\n", " Dhaka: 0 missing hourly timestamps out of 34728\n", " Khulna: 0 missing hourly timestamps out of 34728\n", " Rajshahi: 0 missing hourly timestamps out of 34728\n", " Sylhet: 0 missing hourly timestamps out of 34728\n" ] } ], "source": [ "print(\"Checking for timestamp gaps per station...\")\n", "for station, g in df.groupby(\"station\"):\n", " full_range = pd.date_range(g[\"datetime\"].min(), g[\"datetime\"].max(), freq=\"h\")\n", " missing = full_range.difference(g[\"datetime\"])\n", " print(f\" {station}: {len(missing)} missing hourly timestamps out of {len(full_range)}\")\n", "\n", "# NOTE: if gaps exist, reindex per station and interpolate, e.g.:\n", "# g = g.set_index(\"datetime\").reindex(full_range).interpolate(limit=3)\n", "# Not needed here since this dataset has none in practice." ] }, { "cell_type": "markdown", "id": "06329b3c", "metadata": {}, "source": [ "## 3. Duplicate check" ] }, { "cell_type": "code", "execution_count": 4, "id": "4600653e", "metadata": { "execution": { "iopub.execute_input": "2026-09-11T20:52:51.826023Z", "iopub.status.busy": "2026-09-11T20:52:51.825428Z", "iopub.status.idle": "2026-09-11T20:52:51.866695Z", "shell.execute_reply": "2026-09-11T20:52:51.865909Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Duplicate (station, datetime) rows: 0\n" ] } ], "source": [ "dupes = df.duplicated(subset=[\"station\", \"datetime\"]).sum()\n", "print(f\"Duplicate (station, datetime) rows: {dupes}\")\n", "df = df.drop_duplicates(subset=[\"station\", \"datetime\"])" ] }, { "cell_type": "markdown", "id": "4a5b332d", "metadata": {}, "source": [ "## 4. Outlier capping (winsorize)\n", "Cap pollutant/meteorological columns at the 1st/99th percentile **per station**.\n", "We deliberately do NOT touch PM2.5's upper tail - that's our real spike signal,\n", "capping it would destroy the thing we're trying to predict." ] }, { "cell_type": "code", "execution_count": 5, "id": "5d2ad6b5", "metadata": { "execution": { "iopub.execute_input": "2026-09-11T20:52:51.868593Z", "iopub.status.busy": "2026-09-11T20:52:51.868024Z", "iopub.status.idle": "2026-09-11T20:52:52.014298Z", "shell.execute_reply": "2026-09-11T20:52:52.013134Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Winsorizing done.\n" ] } ], "source": [ "cap_cols = [\"PM10\", \"SO2\", \"NO2\", \"CO\", \"O3\", \"WSPM\"]\n", "\n", "def winsorize_group(g):\n", " for col in cap_cols:\n", " lo, hi = g[col].quantile([0.01, 0.99])\n", " g[col] = g[col].clip(lo, hi)\n", " return g\n", "\n", "# NOTE: must pass df.columns explicitly - newer pandas groupby().apply()\n", "# silently drops the grouping column (\"station\") otherwise.\n", "df = df.groupby(\"station\", group_keys=False)[df.columns].apply(winsorize_group)\n", "\n", "non_negative_cols = [\"PM2.5\", \"PM10\", \"SO2\", \"NO2\", \"CO\", \"O3\", \"WSPM\", \"RAIN\"]\n", "for col in non_negative_cols:\n", " n_neg = (df[col] < 0).sum()\n", " if n_neg > 0:\n", " print(f\"WARNING: {n_neg} negative values in {col}, clipping to 0\")\n", " df[col] = df[col].clip(lower=0)\n", "print(\"Winsorizing done.\")" ] }, { "cell_type": "markdown", "id": "7c0248f5", "metadata": {}, "source": [ "## 5. Time-based train/test split\n", "**Important:** never split randomly for time series data - it leaks future\n", "information into training. Train = before 2026-01-01, Test = 2026 onward." ] }, { "cell_type": "code", "execution_count": 6, "id": "123134a6", "metadata": { "execution": { "iopub.execute_input": "2026-09-11T20:52:52.016723Z", "iopub.status.busy": "2026-09-11T20:52:52.015948Z", "iopub.status.idle": "2026-09-11T20:52:53.923444Z", "shell.execute_reply": "2026-09-11T20:52:53.922204Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train: 179424 rows (2022-08-04 00:00:00 -> 2025-12-31 23:00:00)\n", "Test: 28944 rows (2026-01-01 00:00:00 -> 2026-07-20 23:00:00)\n", "Saved train_clean.csv and test_clean.csv\n" ] } ], "source": [ "SPLIT_DATE = \"2026-01-01\"\n", "train = df[df[\"datetime\"] < SPLIT_DATE].copy()\n", "test = df[df[\"datetime\"] >= SPLIT_DATE].copy()\n", "\n", "print(f\"Train: {train.shape[0]} rows ({train['datetime'].min()} -> {train['datetime'].max()})\")\n", "print(f\"Test: {test.shape[0]} rows ({test['datetime'].min()} -> {test['datetime'].max()})\")\n", "\n", "train.to_csv(\"train_clean.csv\", index=False)\n", "test.to_csv(\"test_clean.csv\", index=False)\n", "print(\"Saved train_clean.csv and test_clean.csv\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.3" } }, "nbformat": 4, "nbformat_minor": 5}123version https://git-lfs.github.com/spec/v1oid sha256:841ce4b5725b599f722295c1eb408e85cf46559422a58b713891c66e5e13832dsize 1799028123version https://git-lfs.github.com/spec/v1oid sha256:c3aaa2e61d844bcc9c082062c0998c10a20afe8b8b5b1bcbe710690a3cfdd2e0size 3189540123version https://git-lfs.github.com/spec/v1oid sha256:dfaeb305d63dd337dd34bb33fd4d23184ea302ce90791e646c5ac0a2dba35b63size 1673748123version https://git-lfs.github.com/spec/v1oid sha256:e0c0a210599c4ff335a50db19ed2ff36546a60937a03fea93a12dc55e89b161dsize 440851123version https://git-lfs.github.com/spec/v1oid sha256:417b4246c2bd62ffa00408763fdf77b22ef490570eb61a07c4827f61e5226d41size 1608980123version https://git-lfs.github.com/spec/v1oid sha256:6becc0e93ac5e0cc881288a7db4ea9bd50d13fbd730ae0ff78956482022b76f3size 1779792notebooks/01_eda.ipynbAdded
notebooks/02_preprocessing.ipynbAdded+647
notebooks/03_feature_engineering.ipynbAdded
notebooks/04_pattern_mining.ipynbAdded
notebooks/05_modeling.ipynbAdded
notebooks/models/lgbm_spike_t1.joblibAdded+3
notebooks/models/lgbm_spike_t2.joblibAdded+3
notebooks/models/lgbm_spike_t3.joblibAdded+3
notebooks/models/xgb_spike_t1.joblibAdded+3
notebooks/models/xgb_spike_t2.joblibAdded+3
notebooks/models/xgb_spike_t3.joblibAdded+3