liveBook · Manning

Geographical clustering

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

Geographical clustering is the grouping of locations according to spatial proximity or similarity to represent geographic patterns.

Geographical clustering groups listings by spatial proximity to expose location patterns that can improve pricing models. A practical approach bins latitude and longitude into a grid, then combines the bin identifiers into one high-cardinality categorical feature. In the Tokyo example, a 32 × 32 grid produces 317 populated geographic values; the resulting feature can later be target encoded in the data pipeline. section 7.1.3

Tokyo listing grid

The binning function divides each coordinate range into 32 intervals. Multiplying the latitude bin by 1,000 and adding the longitude bin creates a unique geographic category for each occupied grid cell. This captures spatial structure without treating every coordinate as a separate continuous value. section 7.1.3

Listing 7.4 Creating a high cardinality geographical feature
def bin_2_cat(feature, bins=32):
    min_value = feature.min()
    bin_size = (feature.max() - min_value) / bins
    return ((feature - min_value) / bin_size).astype(int) #1
    
summary_listings['coordinates'] = (
    bin_2_cat(summary_listings['latitude']) * 
    1000 +   
    bin_2_cat(summary_listings['longitude'])
)                                                         #2
 
print(summary_listings['coordinates'].nunique())

Inspect the Python listing that converts latitude and longitude into the combined geographic feature. section 7.1.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Builds a grid-based feature from coordinate bins and notes later target encoding, but does not develop clustering theory or evaluation.
Examples
Demonstrates a 32 × 32 Tokyo grid that yields 317 geographic categories containing listings.
What it assumes
Assumes listing coordinates and tabular feature-engineering knowledge while demonstrating the coordinate transformation directly.
What you can do after
Build it from scratch by binning latitude and longitude and combining the resulting indices into a categorical model feature.
How it connects
Connects feature engineering with coordinates, high-cardinality categorical features, and target encoding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.