Skip to content

Instantly share code, notes, and snippets.

@acmiyaguchi
Created March 27, 2020 05:03
Show Gist options
  • Select an option

  • Save acmiyaguchi/2e95cd2e845d468d9952db834b7b1fbb to your computer and use it in GitHub Desktop.

Select an option

Save acmiyaguchi/2e95cd2e845d468d9952db834b7b1fbb to your computer and use it in GitHub Desktop.
from sklearn.neighbors import NearestNeighbors
def create_knn_graph(data, n_neighbors):
ann = NearestNeighbors(algorithm="ball_tree", n_jobs=-1)
index = ann.fit(data)
smat = index.kneighbors_graph(data, n_neighbors)
g = nx.from_scipy_sparse_matrix(smat)
print(nx.info(g))
return g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment