3. Outliers and cleaning
Outliers and cleaning
Cleaning never silently drops an order. Each rule either fixes a value, clears one that can't be true, or flags the row for review. Rows only leave the training set at the end, when the model needs a value they don't have.
From the CSV to the training orders
How many orders remain after each step.
36,416 of 45,584 orders (79.9%) reach the model. Cleaning itself removes none.
Show the numbers
| Step | Orders | Share |
|---|---|---|
| Rows in the CSV | 45,584 | 100.0% |
| After cleaning | 45,584 | 100.0% |
| Valid coordinates | 41,513 | 91.1% |
| With a road distance | 37,700 | 82.7% |
| Within 0.1–40 km | 37,614 | 82.5% |
| With an order timestamp | 36,416 | 79.9% |
Cleaning rules
These run in ml/preprocessing.py on every row of the CSV. A rule with no matching rows is still checked, so a new version of the dataset can't slip through.
| Rule | Check | Rows | What happens |
|---|---|---|---|
| Exact duplicate rows | Every field matches an earlier row. | 0 | Dropped |
| Conflicting order IDs | Different rows share the same order ID. | 0 | Kept and flagged |
| Invalid delivery time | Missing, not a number, or zero or less. | 0 | Dropped |
| Long delivery | Delivery took over 60 minutes. | 0 | Kept and flagged for review |
| Restaurant at (0, 0) | Latitude and longitude are both zero, which means the location is unknown. | 3,640 | Set to empty |
| Straight-line distance over 100 km | Over 100 km between restaurant and customer. All of these have a restaurant with negative coordinates. | 431 | Distance set to empty |
| Rider rating above 5 | Ratings must be between 1 and 5. | 53 | Set to empty |
| Rider age out of range | Ages must be whole numbers from 1 to 120. | 0 | Set to empty |
| Rider under 18 | Age is valid but under 18. | 38 | Kept and flagged for review |
| Missing order time | Time_Orderd is blank, so the order hour and day are unknown. | 1,731 | Timestamp set to empty |
| Order time stored as a fraction of a day | Some times are written as 0.5 instead of 12:00. A value of 1 means midnight. | 4,068 | Converted to a clock time |
Rows left out of training
The training script, ml/model_training.py, needs a road distance inside the app's limits and an order time for the chronological split. Rows without them stay in the dataset but aren't used to train or test.
| Reason | Check | Rows | What happens |
|---|---|---|---|
| No road distance | No valid coordinates, no road route, or a point more than 500 m from a road. | 7,884 | Left out of training |
| Road distance outside the app's limits | Under 0.1 km or over 40 km by road. | 86 | Left out of training |
| No usable order timestamp | The chronological split needs the order time. | 1,198 | Left out of training |
Outliers in the numbers
A value counts as an outlier here when it's more than 1.5 times the box width beyond the box. Dots show up to 300 of them. Straight-line distances over 100 km, between 2,216 and 19,693 km, were cleared during cleaning and aren't shown.
Delivery time (min)
45,584 orders. 270 outliers, all of them above the box.
Orders past the right whisker are long but believable: 59% of them were during a festival or with a rider carrying several orders. They're kept, because removing them would teach the model that long deliveries never happen.
Show the numbers
| Statistic | Value |
|---|---|
| Minimum | 10 |
| Lower quartile | 19 |
| Median | 26 |
| Upper quartile | 32 |
| Maximum | 54 |
| Upper fence (1.5 × IQR) | 51.5 |
| Outliers | 270 |
Road distance (km)
37,700 orders. 362 outliers, all of them above the box.
On these routes the road is about 2.0 times the straight-line distance, against 1.4 times for a typical order, so the road network forces a detour. Orders over 40 km by road are left out of training, the same limit the app enforces.
Show the numbers
| Statistic | Value |
|---|---|
| Minimum | 1.486 |
| Lower quartile | 6.982 |
| Median | 12.447 |
| Upper quartile | 18.241 |
| Maximum | 68.461 |
| Upper fence (1.5 × IQR) | 35.13 |
| Outliers | 362 |
Straight-line distance (km)
41,513 orders. No outliers.
After clearing the impossible distances, straight-line distance has no outliers at all. Every remaining order is within about 21 km.
Show the numbers
| Statistic | Value |
|---|---|
| Minimum | 1.465 |
| Lower quartile | 4.658 |
| Median | 9.193 |
| Upper quartile | 13.681 |
| Maximum | 20.97 |
| Upper fence (1.5 × IQR) | 27.216 |
| Outliers | 0 |