Skip to content

About

Predicts truckload freight rates with LightGBM. Handles the data's traps (a quote feature that flips sign by month, 1.4% corrupted labels, unseen cities) and is validated with a rolling-origin backtest: 2.59% MAPE two months ahead vs 4.56% for a lane-median baseline.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Freight Rate Prediction

tests

Predicts the posted rate for a truckload from its lane, equipment, weight and date. The model is trained on 48,000 labelled loads from Jan-Oct 2025 and predicts 12,000 loads from Nov-Dec 2025.

Deliverable Where
Final predictions (load_id,predicted_rate, 12,000 rows) validation_predictions.csv
Filled December chart inputs data/december_chart_inputs.csv
December chart from score.py scorer_results/candidate_december.png
Report (validation and split approach, findings, December chart) reports/Freight_Rate_Model_Report.docx and .pdf

Results

Validated with a rolling-origin backtest: train on every month up to a cut-off, then score the next two months, at six cut-offs. This matches the real task, which predicts two months past the last label.

Design MAPE MAE RMSE Worst fold
Final model (average of two LightGBM members) 2.59% $63 $87 3.79%
Member with market index + elapsed time 2.65% $64 $91 4.53%
Member with lane, load and day of week only 3.47% $85 $114 5.69%
Lane x equipment median baseline 4.56% $110 $155 6.04%

Scores are on test rows that passed label screening. Every number is regenerated by the pipeline into reports/results/.

What the data showed

  • quote_signal is a trap. Its correlation with the true rate per mile is about +1.0 in Jan, Feb, Mar, Jun and Sep, about -0.97 in Apr, May, Jul and Oct, and 0 in Aug. In Nov-Dec it has no relationship with the expected rate at all. A shuffled K-fold makes it look like the best feature (0.98% MAPE). Trained on Jan-Jul and scored on August, it raises the error from 1.47% to 5.04%. It is excluded.
  • The market index is a daily signal with a weekly cycle, and the date explains 98% of its variance. Its seasonal hump does not match the rate level, which climbs through the year. Used raw, it makes the model under-predict autumn, so it is paired with elapsed time and hedged by a second member that ignores it.
  • Data-quality fixes: 677 corrupted rates (1.4%, off by a factor of 2.2-5.5) removed from training. They were found by comparing each rate with an out-of-fold L1 (median) model. Negative weights (sign errors) flipped. Missing market index filled with the same-day mean. Missing weight left to LightGBM.
  • Eight validation cities never appear in training (1,447 rows). City IDs raise unseen-city error from 1.99% to 2.79% on a city hold-out, so location is modelled with coordinates.

Setup

Python 3.12 or newer. The pinned numpy 2.5.5 requires 3.12, so older versions fail at install.

python -m venv .venv
# Windows: .venv\Scripts\activate    macOS/Linux: source .venv/bin/activate
python -m pip install -r requirements.txt

The raw assessment files are not committed. Copy them into data/ under these names:

data/train_test.csv
data/validation.csv
data/validation_predictions_template.csv
data/december_chart_inputs.csv      (already in the repo, filled by the pipeline)

Run

Everything (exploration, validation, final training, both prediction files, score.py, report) takes about four minutes on a laptop:

python -m freight_rate all

Or step by step:

python -m freight_rate eda        # data-quality table, findings, figures         -> reports/
python -m freight_rate validate   # backtest, city hold-out, contrasts            -> reports/results/
python -m freight_rate predict    # fit on all clean data, write predictions      -> validation_predictions.csv,
                                  #                                                  data/december_chart_inputs.csv
python -m freight_rate score      # the provided scorer (same as the command below)
python -m freight_rate report     # rebuild the DOCX report from the results

The scorer can also be run on its own:

python score.py --predictions validation_predictions.csv --december-predictions data/december_chart_inputs.csv

Tests (22: cleaning rules, features, outlier screening, model, and the real outputs checked with the scorer's own validators):

python -m pytest

GitHub Actions runs the tests on every push (badge at the top). The raw data is not in the repo, so CI runs 21 of them and skips the one that compares against the prediction template.

Code map

File What it does
freight_rate/data.py Reads and checks the CSVs, applies the cleaning rules, builds the daily market-index and city-coordinate tables
freight_rate/features.py Model inputs and the target, log(rate per mile); explains the two features left out
freight_rate/outliers.py Screens out corrupted labels with an out-of-fold L1 (median) model
freight_rate/model.py FreightRateModel (the two-member LightGBM average) and the lane-median baseline
freight_rate/evaluation.py Rolling-origin backtest, unseen-city hold-out, random K-fold contrast, August quote check
freight_rate/eda.py Data-quality table, key facts and figures
freight_rate/pipeline.py The steps behind the commands above, including writing both prediction files
freight_rate/report.py Builds the report from reports/results/
score.py Provided scorer, unchanged

Notes

  • The report PDF was exported from the generated DOCX with Word; python -m freight_rate report rebuilds the DOCX.
  • For the December chart, each day's market index is the mean over that day's validation loads. Lexington and Fort Wayne coordinates come from the development data.
  • LightGBM's results are deterministic for a fixed seed and thread count. A different CPU can shift the last decimal of a few predictions.

About

Predicts truckload freight rates with LightGBM. Handles the data's traps (a quote feature that flips sign by month, 1.4% corrupted labels, unseen cities) and is validated with a rolling-origin backtest: 2.59% MAPE two months ahead vs 4.56% for a lane-median baseline.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages