lmcinnes / pynndescent

A Python nearest neighbor descent for approximate nearest neighbors
BSD 2-Clause "Simplified" License
899 stars 105 forks source link

Scripts for making plots on pynndescent docs #158

Closed rhysnewell closed 2 years ago

rhysnewell commented 2 years ago

Hi Leland,

This isn't an issue just a special request. I've been trying to emulate the plots you show here for a small section of a figure I'm trying to make: https://pynndescent.readthedocs.io/en/latest/how_pynndescent_works.html I was just wondering if you would be able to share the scripts you used to plot out your nearest neighbour graphs so uniformly and neatly. I've been trying to wrangle networkx and it really isn't working out, the graph layouts always seem so wrong.

Cheers, Rhys

rhysnewell commented 2 years ago

My current solution is something like the following:

import networkx as nx
import numpy as np
from pynndescent import NNDescent

index_euc = NNDescent(data, metric="euclidean", n_neighbors=30)

# Create empy adjacency matrix
net = np.zeros((index_euc._neighbor_graph[0].shape[0], index_euc._neighbor_graph[0].shape[0]))
# Fill adjacency matrix with connections
for row in range(index_euc._neighbor_graph[0].shape[0]):
    net[row, index_euc._neighbor_graph[0][row,1:]] = 1
np.fill_diagonal(net, 0)   

# Create netowrkx graph
G = nx.convert_matrix.from_numpy_array(net)

But this graph often looks odd when plotted: image

I realize that this is partially due to the lack of connections in the graph, but I'm just wondering if you had any tricks to make it plot out nicer

lmcinnes commented 2 years ago

A large part of it is that I plot the data spatially, and just add the edges to that. From there it is just a matter of generating nice 2D data to work with. Sadly I lost the notebooks I had for generating those plots in a hard drive failure, but it was all standard matplotlib with no requirement on networkX.

rhysnewell commented 2 years ago

Okay nice, I'll keep trying at it then. Thanks for your help! And sorry to hear about your harddrive failure, that would have been super stressful. Closed