Predict delivery times at checkout
Your logistics data already records how long past shipments took, by distance, carrier and weight. Spavik learns from it and estimates the delivery time of each new parcel, so customers see a realistic estimate before they pay.
- Returns
- An estimated delivery time, in hours
- Language
- JavaScript
In your app
Parcel #A1042
estimated delivery time
Show the estimated delivery time at checkout.
The data you need
One row per past case. Spavik learns how the inputs relate to the column you want to predict.
Past shipments, with distance in miles, carrier, weight in pounds, dispatch day and delivery time in hours.
| Column | Role | Meaning |
|---|---|---|
distance_miles | Input | Distance in miles |
carrier | Input | Carrier or service level |
weight_lb | Input | Parcel weight in pounds |
dispatch_day | Input | Day the parcel leaves |
hours | Predicted | Delivery time in hours |
The code
A complete, commented script. Set SPAVIK_API_KEY in your server environment, never in browser code.
import { Spavik } from 'spavik';
// Set SPAVIK_API_KEY in your server environment.
const spavik = new Spavik();
// shipments.csv: distance_miles, carrier, weight_lb,
// dispatch_day, hours (delivery time in hours).
const history = 'shipments.csv';
const report = await spavik.evaluate(history, {
target: 'hours'
});
const { status, message } = report.assessment as {
status: string; message: string;
};
if (status !== 'better_than_baseline') {
throw new Error(message);
}
// Create once, then reuse for every new parcel.
const model = await spavik.train(history, {
target: 'hours'
});
// Parcel #A1042, as known at checkout.
const parcel = {
distance_miles: 400, carrier: 'ground',
weight_lb: 5.3, dispatch_day: 'friday'
};
const result = await model.predict(parcel);
// Show the estimate at checkout.
const hours = Math.round(Number(result.lignes[0].prediction));
console.log(`Parcel #A1042: about ${hours} hours`);
How it works
- 1
Check your data
evaluate()tells you, for free, whether your history does better than a simple guess. - 2
Learn from your history
train()creates the model once, for free. Keep it and reuse it for every new case. - 3
Ask for predictions
predict()returns a value for each new row, from your server. One credit per row. - 4
Keep it accurate
Report what actually happened with
submit_outcomes(), checkperformance(), thenrefresh().
Questions
How much history do I need?
At least 10 rows, with at least 2 different values in the column you predict. Keep only fields known at checkout, before the parcel ships. evaluate() tells you for free whether your history does better than a simple guess before you train anything.
What does it cost?
Checking your data with evaluate() and creating the model with train() are free. Each predicted row costs one credit on the default engine. You train once and keep the model, so you pay only for the predictions you request.