Add the distance from each row to a fixed reference point.
StepGeoDistance(
lat,
lon,
*,
ref_lat,
ref_lon,
metric="haversine",
log_distance=False,
name="geo_dist",
keep_original_cols=False,
role="feature",
id=None
)
Replaces the lat/lon columns with a single name column holding each row’s distance to (ref_lat, ref_lon). With the default "haversine" metric the result is the great-circle distance in meters; with "euclidean" it is the flat-plane distance between the raw lat/lon values.
See Spatial Distance in Feature Engineering A to Z for background on this technique.
See Arguments shared across steps for the arguments this step shares with the others.
Parameters
lat: str
-
Name of the latitude column.
lon: str
-
Name of the longitude column.
ref_lat: float
-
Latitude of the reference point. Must be between -90 and 90 when metric is "haversine".
ref_lon: float
-
Longitude of the reference point. Must be between -180 and 180 when metric is "haversine".
metric: GeoMetric = "haversine"
-
Distance function: "haversine" (default) for great-circle distance in meters, or "euclidean" for the flat-plane distance between the raw lat/lon values.
log_distance: bool = False
-
Whether to return log(distance) instead of the raw distance. A row exactly at the reference point has distance 0, which gives -inf; there is no eps offset as in StepClassDistance. Default False.
name: str = "geo_dist"
-
Name of the output distance column. Default "geo_dist". A Recipe rejects the step if the data frame already has a column of this name.
keep_original_cols: bool = False
-
If True, the lat/lon columns are kept alongside the distance column instead of being dropped. Default False.
role: str | None = "feature"
-
Role assigned to the new distance column (see Roles). Default "feature".
id: str | None = None
-
Stable step identifier. Defaults to
"geo_distance"; a second step of this type in the same recipe becomes "geo_distance_2", and passing an id already used in the recipe is an error.
Examples
Each row’s distance to Union Station in Washington, D.C.:
df = pd.DataFrame(
{
"name": ["Union Station", "Smithsonian Castle"],
"lat": [38.897303, 38.888504],
"lon": [-77.006430, -77.026352],
}
)
df
|
name |
lat |
lon |
| 0 |
Union Station |
38.897303 |
-77.006430 |
| 1 |
Smithsonian Castle |
38.888504 |
-77.026352 |
Specifying the step touches no data, so the recipe reports itself as untrained:
recipe = Recipe().add(
StepGeoDistance("lat", "lon", ref_lat=38.8986312, ref_lon=-77.0062457)
)
recipe
Recipe (untrained)
Inputs: no roles assigned
Steps:
step_geo_distance(['lat', 'lon']) [untrained]
fit() records the reference point and returns a trained recipe:
fitted = recipe.fit(df)
fitted
Recipe (trained)
Inputs: 2 columns (2 features)
Steps:
step_geo_distance(lat, lon → geo_dist) [trained]
transform() replaces lat/lon with the geo_dist column, in meters:
|
name |
geo_dist |
| 0 |
Union Station |
148.547793 |
| 1 |
Smithsonian Castle |
2072.679445 |
metric="euclidean" computes the flat-plane distance between the raw coordinate values instead:
recipe = Recipe().add(
StepGeoDistance(
"lat", "lon", ref_lat=38.8986312, ref_lon=-77.0062457, metric="euclidean"
)
)
fitted = recipe.fit(df)
fitted.transform(df)
|
name |
geo_dist |
| 0 |
Union Station |
0.001341 |
| 1 |
Smithsonian Castle |
0.022513 |
keep_original_cols keeps lat/lon alongside the distance:
recipe = Recipe().add(
StepGeoDistance(
"lat",
"lon",
ref_lat=38.8986312,
ref_lon=-77.0062457,
keep_original_cols=True,
)
)
fitted = recipe.fit(df)
fitted.transform(df)
|
name |
lat |
lon |
geo_dist |
| 0 |
Union Station |
38.897303 |
-77.006430 |
148.547793 |
| 1 |
Smithsonian Castle |
38.888504 |
-77.026352 |
2072.679445 |