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
