Skip to content
ML Academy

Feature engineering and preprocessing · insight-1722

Compute distances from coordinates with a great-circle formula in radians, not Euclidean on degrees

Evidence type
practitioner heuristic
Difficulty
easy
Roles
Data scientist, Data analyst
Industries
aviation, logistics

Summary: situation, action, reason, boundary

  1. Situation

    A model needs distance between origins and destinations, or a nearest-station feature, from latitude and longitude columns, and the step runs on raw degree values.

  2. Action

    Use a haversine distance on radians in latitude, longitude order, multiply by an Earth radius for kilometres, and unit-test pairs of known distance, including one across the date line.

  3. Reason

    scikit-learn's haversine_distances expects (latitude, longitude) in radians and returns an angle that the example multiplies by 6,371 km.

  4. Boundary

    Route length, hub proximity and weather-station joins; clustering on coordinates.

Symptom or risk

A degree of longitude is shorter than a degree of latitude away from the equator and longitudes wrap at 180, so east-west distances are inflated and points across the date line look far apart.

Exceptions

Small areas where a local projection is used on purpose, with the projection recorded.

Trade-offs

Haversine treats the Earth as a sphere; use a geodesic library where metres matter.

Pitfalls

Passing (longitude, latitude); forgetting radians.

Example

Computed: two points on 41 N, 10 degrees of longitude apart, are about 839 km apart, not the 1,112 km that 111.2 km per degree suggests. Points at 179 E and 179 W on the equator are 222 km apart but 358 degrees apart in raw longitude.

Lessons and concepts

Claims and their sources