Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples to from_dicts and build_knn. #704

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,51 @@ def from_dicts(cls, neighbors, weights=None):
-------
Graph
libpysal.graph.Graph based on dictionaries

Examples
--------
>>> neighbors = {
... 'Africa': ['Asia'],
... 'Asia': ['Africa', 'Europe'],
... 'Australia': [],
... 'Europe': ['Asia'],
... 'North America': ['South America'],
... 'South America': ['North America'],
... }
>>> connectivity = graph.Graph.from_dicts(neighbors)
>>> connectivity.adjacency
focal neighbor
Africa Asia 1
Asia Africa 1
Europe 1
Australia Australia 0
Europe Asia 1
North America South America 1
South America North America 1
Name: weight, dtype: float64

You can also specify weights (for example based
on the length of the shared border):

>>> weights = {
... 'Africa': [1],
... 'Asia': [0.2, 0.8],
... 'Australia': [],
... 'Europe': [1],
... 'North America': [1],
... 'South America': [1],
... }
>>> connectivity = graph.Graph.from_dicts(neighbors, weights)
>>> connectivity.adjacency
focal neighbor
Africa Asia 1.0
Asia Africa 0.2
Europe 0.8
Australia Australia 0.0
Europe Asia 1.0
North America South America 1.0
South America North America 1.0
Name: weight, dtype: float64
"""
head, tail, weight = _neighbor_dict_to_edges(neighbors, weights=weights)
return cls.from_arrays(head, tail, weight)
Expand Down Expand Up @@ -685,6 +730,53 @@ def build_knn(cls, data, k, metric="euclidean", p=2, coincident="raise"):
-------
Graph
libpysal.graph.Graph encoding KNN weights

Examples
--------
>>> import geopandas as gpd
>>> from geodatasets import get_path
>>> nybb = gpd.read_file(get_path('nybb')).set_index('BoroName')
>>> nybb
BoroCode ... geometry
BoroName ...
Staten Island 5 ... MULTIPOLYGON (((970217.022 145643.332, 970227....
Queens 4 ... MULTIPOLYGON (((1029606.077 156073.814, 102957...
Brooklyn 3 ... MULTIPOLYGON (((1021176.479 151374.797, 102100...
Manhattan 1 ... MULTIPOLYGON (((981219.056 188655.316, 980940....
Bronx 2 ... MULTIPOLYGON (((1012821.806 229228.265, 101278...

>>> knn3 = graph.Graph.build_knn(nybb.centroid, k=3)
>>> knn3.adjacency
focal neighbor
Staten Island Queens 1
Brooklyn 1
Manhattan 1
Queens Brooklyn 1
Manhattan 1
Bronx 1
Brooklyn Staten Island 1
Queens 1
Manhattan 1
Manhattan Queens 1
Brooklyn 1
Bronx 1
Bronx Queens 1
Brooklyn 1
Manhattan 1
Name: weight, dtype: int32

Specifying k=1 identifies the nearest neighbor
(note that this can be asymmetrical):

>>> knn1 = graph.Graph.build_knn(nybb.centroid, k=1)
>>> knn1.adjacency
focal neighbor
Staten Island Brooklyn 1
Queens Brooklyn 1
Brooklyn Queens 1
Manhattan Bronx 1
Bronx Manhattan 1
Name: weight, dtype: int32
"""
ids = _evaluate_index(data)

Expand Down