Hex2vec embedder
In [1]:
Copied!
from srai.embedders import Hex2VecEmbedder
from srai.joiners import IntersectionJoiner
from srai.loaders import OSMOnlineLoader
from srai.neighbourhoods import H3Neighbourhood
from srai.regionalizers import H3Regionalizer, geocode_to_region_gdf
from srai.plotting import plot_regions, plot_numeric_data
from pytorch_lightning import seed_everything
from srai.embedders import Hex2VecEmbedder
from srai.joiners import IntersectionJoiner
from srai.loaders import OSMOnlineLoader
from srai.neighbourhoods import H3Neighbourhood
from srai.regionalizers import H3Regionalizer, geocode_to_region_gdf
from srai.plotting import plot_regions, plot_numeric_data
from pytorch_lightning import seed_everything
In [2]:
Copied!
SEED = 71
seed_everything(SEED)
SEED = 71
seed_everything(SEED)
Seed set to 71
Out[2]:
71
Load data from OSM¶
First use geocoding to get the area
In [3]:
Copied!
area_gdf = geocode_to_region_gdf("Wrocław, Poland")
plot_regions(area_gdf, tiles_style="CartoDB positron")
area_gdf = geocode_to_region_gdf("Wrocław, Poland")
plot_regions(area_gdf, tiles_style="CartoDB positron")
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Next, download the data for the selected region and the specified tags. We're using OSMOnlineLoader
here, as it's faster for low numbers of tags. In a real life scenario with more tags, you would likely want to use the OSMPbfLoader
.
In [4]:
Copied!
tags = {
"leisure": "park",
"landuse": "forest",
"amenity": ["bar", "restaurant", "cafe"],
"water": "river",
"sport": "soccer",
}
loader = OSMOnlineLoader()
features_gdf = loader.load(area_gdf, tags)
folium_map = plot_regions(area_gdf, colormap=["rgba(0,0,0,0)"], tiles_style="CartoDB positron")
features_gdf.explore(m=folium_map)
tags = {
"leisure": "park",
"landuse": "forest",
"amenity": ["bar", "restaurant", "cafe"],
"water": "river",
"sport": "soccer",
}
loader = OSMOnlineLoader()
features_gdf = loader.load(area_gdf, tags)
folium_map = plot_regions(area_gdf, colormap=["rgba(0,0,0,0)"], tiles_style="CartoDB positron")
features_gdf.explore(m=folium_map)
Downloading sport: soccer : 100%|██████████| 7/7 [00:04<00:00, 1.68it/s]
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Prepare the data for embedding¶
After downloading the data, we need to prepare it for embedding. Namely - we need to regionalize the selected area, and join the features with regions.
In [5]:
Copied!
regionalizer = H3Regionalizer(resolution=9)
regions_gdf = regionalizer.transform(area_gdf)
plot_regions(regions_gdf, tiles_style="CartoDB positron")
regionalizer = H3Regionalizer(resolution=9)
regions_gdf = regionalizer.transform(area_gdf)
plot_regions(regions_gdf, tiles_style="CartoDB positron")
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [6]:
Copied!
joiner = IntersectionJoiner()
joint_gdf = joiner.transform(regions_gdf, features_gdf)
joint_gdf
joiner = IntersectionJoiner()
joint_gdf = joiner.transform(regions_gdf, features_gdf)
joint_gdf
Out[6]:
region_id | feature_id |
---|---|
891e20519abffff | way/302673833 |
891e2051933ffff | way/302673833 |
891e20519afffff | way/302673833 |
891e2051907ffff | way/302673833 |
891e2042653ffff | way/1087563713 |
... | ... |
891e2040c43ffff | way/769920653 |
node/2451527821 | |
node/4681338234 | |
891e2041d23ffff | way/628674333 |
891e2043593ffff | way/316195012 |
3796 rows × 0 columns
Embedding¶
After preparing the data we can proceed with generating embeddings for the regions.
In [7]:
Copied!
import warnings
neighbourhood = H3Neighbourhood(regions_gdf)
embedder = Hex2VecEmbedder([15, 10])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
embeddings = embedder.fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
trainer_kwargs={"max_epochs": 5, "accelerator": "cpu"},
batch_size=100,
)
embeddings
import warnings
neighbourhood = H3Neighbourhood(regions_gdf)
embedder = Hex2VecEmbedder([15, 10])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
embeddings = embedder.fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
trainer_kwargs={"max_epochs": 5, "accelerator": "cpu"},
batch_size=100,
)
embeddings
100%|██████████| 3168/3168 [00:00<00:00, 14672.95it/s] GPU available: False, used: False TPU available: False, using: 0 TPU cores IPU available: False, using: 0 IPUs HPU available: False, using: 0 HPUs | Name | Type | Params --------------------------------------- 0 | encoder | Sequential | 280 --------------------------------------- 280 Trainable params 0 Non-trainable params 280 Total params 0.001 Total estimated model params size (MB)
`Trainer.fit` stopped: `max_epochs=5` reached.
Out[7]:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
region_id | ||||||||||
891e20519abffff | -0.333819 | 0.064100 | -0.319463 | -0.486495 | 0.015318 | 0.244642 | -0.377593 | 0.334572 | -0.087357 | -0.016808 |
891e2055283ffff | 0.353454 | -0.241863 | -0.026217 | 0.279467 | 0.227001 | -0.133161 | -0.253003 | -0.116493 | -0.201177 | 0.348441 |
891e2042653ffff | -0.233576 | -0.043678 | 0.000542 | -0.276059 | -0.022181 | 0.130279 | -0.013310 | 0.164849 | 0.079497 | -0.150135 |
891e2042cdbffff | 0.353454 | -0.241863 | -0.026217 | 0.279467 | 0.227001 | -0.133161 | -0.253003 | -0.116493 | -0.201177 | 0.348441 |
891e204085bffff | -0.757970 | 1.528926 | 1.179453 | 1.426461 | -0.839417 | -0.020052 | 1.370004 | -0.807247 | 1.271383 | -0.244897 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
891e205a92fffff | 0.353454 | -0.241863 | -0.026217 | 0.279467 | 0.227001 | -0.133161 | -0.253003 | -0.116493 | -0.201177 | 0.348441 |
891e20414abffff | -0.333819 | 0.064100 | -0.319463 | -0.486495 | 0.015318 | 0.244642 | -0.377593 | 0.334572 | -0.087357 | -0.016808 |
891e2042107ffff | -0.333819 | 0.064100 | -0.319463 | -0.486495 | 0.015318 | 0.244642 | -0.377593 | 0.334572 | -0.087357 | -0.016808 |
891e2043593ffff | -0.470607 | 0.210630 | -0.397213 | -0.615241 | -0.067614 | 0.281458 | -0.356275 | 0.423573 | -0.115090 | -0.141166 |
891e204554fffff | -0.470607 | 0.210630 | -0.397213 | -0.615241 | -0.067614 | 0.281458 | -0.356275 | 0.423573 | -0.115090 | -0.141166 |
3168 rows × 10 columns
Visualizing the embeddings' similarity¶
In [8]:
Copied!
from sklearn.cluster import KMeans
clusterizer = KMeans(n_clusters=5, random_state=SEED)
clusterizer.fit(embeddings)
embeddings["cluster"] = clusterizer.labels_
embeddings
from sklearn.cluster import KMeans
clusterizer = KMeans(n_clusters=5, random_state=SEED)
clusterizer.fit(embeddings)
embeddings["cluster"] = clusterizer.labels_
embeddings
/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning super()._check_params_vs_input(X, default_n_init=10)
Out[8]:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | cluster | |
---|---|---|---|---|---|---|---|---|---|---|---|
region_id | |||||||||||
891e20519abffff | -0.333819 | 0.064100 | -0.319463 | -0.486495 | 0.015318 | 0.244642 | -0.377593 | 0.334572 | -0.087357 | -0.016808 | 1 |
891e2055283ffff | 0.353454 | -0.241863 | -0.026217 | 0.279467 | 0.227001 | -0.133161 | -0.253003 | -0.116493 | -0.201177 | 0.348441 | 0 |
891e2042653ffff | -0.233576 | -0.043678 | 0.000542 | -0.276059 | -0.022181 | 0.130279 | -0.013310 | 0.164849 | 0.079497 | -0.150135 | 1 |
891e2042cdbffff | 0.353454 | -0.241863 | -0.026217 | 0.279467 | 0.227001 | -0.133161 | -0.253003 | -0.116493 | -0.201177 | 0.348441 | 0 |
891e204085bffff | -0.757970 | 1.528926 | 1.179453 | 1.426461 | -0.839417 | -0.020052 | 1.370004 | -0.807247 | 1.271383 | -0.244897 | 2 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
891e205a92fffff | 0.353454 | -0.241863 | -0.026217 | 0.279467 | 0.227001 | -0.133161 | -0.253003 | -0.116493 | -0.201177 | 0.348441 | 0 |
891e20414abffff | -0.333819 | 0.064100 | -0.319463 | -0.486495 | 0.015318 | 0.244642 | -0.377593 | 0.334572 | -0.087357 | -0.016808 | 1 |
891e2042107ffff | -0.333819 | 0.064100 | -0.319463 | -0.486495 | 0.015318 | 0.244642 | -0.377593 | 0.334572 | -0.087357 | -0.016808 | 1 |
891e2043593ffff | -0.470607 | 0.210630 | -0.397213 | -0.615241 | -0.067614 | 0.281458 | -0.356275 | 0.423573 | -0.115090 | -0.141166 | 1 |
891e204554fffff | -0.470607 | 0.210630 | -0.397213 | -0.615241 | -0.067614 | 0.281458 | -0.356275 | 0.423573 | -0.115090 | -0.141166 | 1 |
3168 rows × 11 columns
In [9]:
Copied!
plot_numeric_data(regions_gdf, "cluster", embeddings)
plot_numeric_data(regions_gdf, "cluster", embeddings)
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook