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
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.
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.
Reason
scikit-learn's haversine_distances expects (latitude, longitude) in radians and returns an angle that the example multiplies by 6,371 km.
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
The scikit-learn haversine_distances page takes two columns of (latitude, longitude) in radians, returns the angular great-circle distance, and shows converting degrees with math.radians and multiplying by an Earth radius of 6,371 km for kilometres.