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.

Rows in the CSV
45,584
After cleaning
45,584
Valid coordinates
41,513
With a road distance
37,700
Within 0.1–40 km
37,614
With an order timestamp
36,416

36,416 of 45,584 orders (79.9%) reach the model. Cleaning itself removes none.

Show the numbers
StepOrdersShare
Rows in the CSV45,584100.0%
After cleaning45,584100.0%
Valid coordinates41,51391.1%
With a road distance37,70082.7%
Within 0.1–40 km37,61482.5%
With an order timestamp36,41679.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.

RuleCheckRowsWhat happens
Exact duplicate rowsEvery field matches an earlier row.0Dropped
Conflicting order IDsDifferent rows share the same order ID.0Kept and flagged
Invalid delivery timeMissing, not a number, or zero or less.0Dropped
Long deliveryDelivery took over 60 minutes.0Kept and flagged for review
Restaurant at (0, 0)Latitude and longitude are both zero, which means the location is unknown.3,640Set to empty
Straight-line distance over 100 kmOver 100 km between restaurant and customer. All of these have a restaurant with negative coordinates.431Distance set to empty
Rider rating above 5Ratings must be between 1 and 5.53Set to empty
Rider age out of rangeAges must be whole numbers from 1 to 120.0Set to empty
Rider under 18Age is valid but under 18.38Kept and flagged for review
Missing order timeTime_Orderd is blank, so the order hour and day are unknown.1,731Timestamp set to empty
Order time stored as a fraction of a daySome times are written as 0.5 instead of 12:00. A value of 1 means midnight.4,068Converted 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.

ReasonCheckRowsWhat happens
No road distanceNo valid coordinates, no road route, or a point more than 500 m from a road.7,884Left out of training
Road distance outside the app's limitsUnder 0.1 km or over 40 km by road.86Left out of training
No usable order timestampThe chronological split needs the order time.1,198Left 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.

All orders

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
StatisticValue
Minimum10
Lower quartile19
Median26
Upper quartile32
Maximum54
Upper fence (1.5 × IQR)51.5
Outliers270

Road distance (km)

37,700 orders. 362 outliers, all of them above the box.

All orders

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
StatisticValue
Minimum1.486
Lower quartile6.982
Median12.447
Upper quartile18.241
Maximum68.461
Upper fence (1.5 × IQR)35.13
Outliers362

Straight-line distance (km)

41,513 orders. No outliers.

All orders

After clearing the impossible distances, straight-line distance has no outliers at all. Every remaining order is within about 21 km.

Show the numbers
StatisticValue
Minimum1.465
Lower quartile4.658
Median9.193
Upper quartile13.681
Maximum20.97
Upper fence (1.5 × IQR)27.216
Outliers0