5. Features and training
Features, pipeline and tuning
Each model gets the same six inputs and the same preprocessing. Only the final regressor changes. Settings are chosen on the validation orders, never on the test orders.
The six inputs
Every input is something the app knows the moment the customer presses predict. Importance is how much the validation error grows, in minutes, when that input's values are shuffled between orders.
| Input | Type | Where the app gets it | Importance (XGBoost) |
|---|---|---|---|
| Order hour | Number | Order time in the restaurant's time zone | +1.07 min |
| Road distance (km) | Number | OSRM road route from the restaurant to the pin | +0.55 min |
| Festival | Category | Festival checkbox on the predict page | +0.53 min |
| City type | Category | Stored on the restaurant | +0.32 min |
| Order type | Category | Order type control on the predict page | +0.00 min |
| Day of week | Number | Order date in the restaurant's time zone | +0.00 min |
Why road distance instead of a straight line
The dataset only has coordinates. The first version of the model used the straight-line distance between them. But a rider follows roads, and the app measures the actual route on the map, so a model trained on straight lines would receive a different kind of number in the app than it learned from. Training now uses the same OSRM road route the app shows, added to every order by ml.scripts.enrich_routes.
In this dataset both distances relate to delivery time about equally (correlation 0.32 for the straight line and 0.31 for the road), so the switch isn't about squeezing out accuracy. It makes training and the app measure the same thing.
The preprocessing pipeline
ml/pipeline.py builds one scikit-learn pipeline. It's fitted on training orders only and saved whole, so the API transforms each order exactly as training did.
median"unknown", then one-hot encode. New labels are ignored.Hyperparameter tuning
Each model is fitted once per combination in its grid and scored on the validation orders. The best combination is then refitted on training and validation orders together before the final test. For reference, the median baseline has a validation error of 7.77 min.
Decision Tree
Depth 3, 5 or 8 × at least 20 or 80 orders per leaf: 6 combinations.
Best: depth 8, min leaf 20, validation MAE 6.25 min. The worst setting is 0.34 min worse.
Show the numbers
| Settings | MAE | RMSE | R² |
|---|---|---|---|
| depth 8, min leaf 20 | 6.252 | 7.836 | 0.335 |
| depth 5, min leaf 20 | 6.326 | 7.943 | 0.317 |
| depth 8, min leaf 80 | 6.328 | 7.941 | 0.317 |
| depth 5, min leaf 80 | 6.383 | 8.021 | 0.303 |
| depth 3, min leaf 20 | 6.557 | 8.251 | 0.263 |
| depth 3, min leaf 80 | 6.593 | 8.282 | 0.257 |
XGBoostSelected
Depth 3 or 5 × learning rate 0.03 or 0.1 × 150 or 300 trees: 8 combinations.
Best: learning rate 0.03, depth 5, trees 300, validation MAE 6.16 min. The worst setting is 0.07 min worse.
Show the numbers
| Settings | MAE | RMSE | R² |
|---|---|---|---|
| learning rate 0.03, depth 5, trees 300 | 6.160 | 7.711 | 0.356 |
| learning rate 0.1, depth 3, trees 150 | 6.162 | 7.716 | 0.355 |
| learning rate 0.03, depth 3, trees 300 | 6.167 | 7.718 | 0.355 |
| learning rate 0.03, depth 5, trees 150 | 6.169 | 7.716 | 0.355 |
| learning rate 0.1, depth 3, trees 300 | 6.172 | 7.721 | 0.354 |
| learning rate 0.1, depth 5, trees 150 | 6.175 | 7.725 | 0.354 |
| learning rate 0.1, depth 5, trees 300 | 6.213 | 7.777 | 0.345 |
| learning rate 0.03, depth 3, trees 150 | 6.231 | 7.782 | 0.344 |
Feature importance
How much validation MAE grows when each input is shuffled, averaged over five shuffles. Larger means the model relies on it more.
- Decision Tree
- XGBoost
Order hour matters most, then road distance and festival. Order type and day of the week barely matter, which matches the exploratory analysis.
Show the numbers
| Input | Decision Tree | XGBoost |
|---|---|---|
| Order hour | 1.134 min | 1.066 min |
| Road distance (km) | 0.604 min | 0.551 min |
| Festival | 0.433 min | 0.526 min |
| City type | 0.267 min | 0.322 min |
| Order type | -0.005 min | 0.002 min |
| Day of week | -0.002 min | 0.002 min |