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

31 hours

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.

ColumnRoleMeaning
distance_milesInputDistance in miles
carrierInputCarrier or service level
weight_lbInputParcel weight in pounds
dispatch_dayInputDay the parcel leaves
hoursPredictedDelivery time in hours

The code

A complete, commented script. Set SPAVIK_API_KEY in your server environment, never in browser code.

delivery-time-prediction.ts JavaScript
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. 1

    Check your data

    evaluate() tells you, for free, whether your history does better than a simple guess.

  2. 2

    Learn from your history

    train() creates the model once, for free. Keep it and reuse it for every new case.

  3. 3

    Ask for predictions

    predict() returns a value for each new row, from your server. One credit per row.

  4. 4

    Keep it accurate

    Report what actually happened with submit_outcomes(), check performance(), then refresh().

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.

Try it on your own data

Get an API key. Checking your data is free.