This is documentation for an old release of Scikit-learn (version 1.2). Try the latest stable release (version 1.6) or development (unstable) versions.

sklearn.metrics.pairwise.haversine_distances

sklearn.metrics.pairwise.haversine_distances(X, Y=None)[source]

Compute the Haversine distance between samples in X and Y.

The Haversine (or great circle) distance is the angular distance between two points on the surface of a sphere. The first coordinate of each point is assumed to be the latitude, the second is the longitude, given in radians. The dimension of the data must be 2.

D(x,y)=2arcsin[sin2((x1y1)/2)+cos(x1)cos(y1)sin2((x2y2)/2)]
Parameters:
Xarray-like of shape (n_samples_X, 2)

A feature array.

Yarray-like of shape (n_samples_Y, 2), default=None

An optional second feature array. If None, uses Y=X.

Returns:
distancendarray of shape (n_samples_X, n_samples_Y)

The distance matrix.

Notes

As the Earth is nearly spherical, the haversine formula provides a good approximation of the distance between two points of the Earth surface, with a less than 1% error on average.

Examples

We want to calculate the distance between the Ezeiza Airport (Buenos Aires, Argentina) and the Charles de Gaulle Airport (Paris, France).

>>>
>>> from sklearn.metrics.pairwise import haversine_distances
>>> from math import radians
>>> bsas = [-34.83333, -58.5166646]
>>> paris = [49.0083899664, 2.53844117956]
>>> bsas_in_radians = [radians(_) for _ in bsas]
>>> paris_in_radians = [radians(_) for _ in paris]
>>> result = haversine_distances([bsas_in_radians, paris_in_radians])
>>> result * 6371000/1000  # multiply by Earth radius to get kilometers
array([[    0.        , 11099.54035582],
       [11099.54035582,     0.        ]])