Skip to main content

Import

from air_travel_model import TransportPredictor
tp = TransportPredictor()
On instantiation, TransportPredictor loads all available .pkl model files from src/air_travel_model/data/. Missing files are skipped — the predictor still works with whatever models are present.

Methods

predict(model, origin, destination, **kwargs) -> float | None

Runs the ML model for the given mode. Returns a float (time in minutes or cost in USD), or None if the model is not loaded. For drive_time, routes not in the training dataset fall back to a live OSRM query before returning None. See Routing Fallback.
fare     = tp.predict("air_fare",   "CHS", "ROA", quarter=2, is_business=False)
air_min  = tp.predict("air_time",   "CHS", "ROA", quarter=2)
drv_min  = tp.predict("drive_time", "CHS", "ROA")
ParameterTypeDescription
modelstrModel name — see table below
originstr | tupleIATA code, "City, ST", or (lat, lon)
destinationstr | tupleSame as origin
**kwargsModel-specific options — see table below
Raises ValueError for unrecognised location strings.

estimate(model, origin, destination, **kwargs) -> float | None

Heuristic estimate — intended as a guaranteed fallback when predict() returns None or the model is not loaded. For drive_time, estimate() also queries OSRM first and only falls back to the haversine formula if OSRM is unavailable.
result = tp.estimate("drive_time", "BFD", "UNV")   # always returns a value

predict_all(origin, destination, **kwargs) -> dict

Calls predict() for all loaded models and returns a {model_name: float} dict. Models that return None are omitted from the result.
results = tp.predict_all("CRW", "ATL", quarter=2)
# {"air_fare": 253.74, "air_time": 59.0, "drive_time": 595.0, "drive_fare": 58.49}

estimate_all(origin, destination) -> dict

Calls estimate() for all models. Returns a {model_name: float} dict, skipping any model whose estimate() raises or returns None.

available -> list[str]

Returns model names that have a trained .pkl loaded.
print(tp.available)
# ["air_fare", "air_time", "drive_time", "drive_fare", ...]

Model names and kwargs

Model nameUnitRequired kwargsOptional kwargs
air_fareUSDquarter (1–4)is_business (bool, default False)
air_timeminutesquarter (1–4)
drive_timeminutes
drive_fareUSDgas_price ($/gal, default 3.50), mpg (default 28)
bus_fareUSDquarter (1–4)is_premium (bool, default False)
bus_timeminutesquarter (1–4)
train_timeminutesquarter (1–4)

Location resolution

Locations are resolved through air_travel_model.resolvers.location.resolve():
  • IATA code ("CHS") — direct lookup in airports.csv
  • City name ("Charleston, SC") — matched against city, state entries in airports.csv
  • (lat, lon) tuple — used directly for coordinate-based routing; no IATA code is assigned, so models that require a code for route-stats lookup (air_fare, air_time) will fall back to their heuristics
Raises ValueError with a descriptive message for unrecognised strings. For train_time, origins and destinations are additionally resolved against amtrak_stations.csv. Amtrak station codes ("NYP", "WAS", "CHI") give the best accuracy because the model was trained on station-to-station pairs.