Feature request: plot graph to visualise distance
See original GitHub issueI’ve always been a fan of using networkx to create graphs for my vector embeddings. The networkx implementation of the kamada kawai graph layout allows you to input a dictionary containing the distance of each node to the others (eg: {0: {0:0, 1: .01}, 1: {1:0, 0:, 0.1}}), which it then tries to visualise in a good way. So what I often do is calculate for example the cosine distances to be used as input. This can give a nice visual on the similarity based on distance:
My quick code for this on the embedding set (which doesn’t work for the api you made but I’ll leave that to you 😃 ):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def plot_graph(self):
plt.figure(figsize=(10, 10))
vectors = [token.vector for k, token in self.embeddings.items()]
label_dict = {i: w for i, (w, _) in enumerate(self.embeddings.items())}
dist = cosine_distances(np.array(vectors), np.array(vectors))
# Greate graph
G = nx.from_numpy_matrix(dist)
distance = pd.DataFrame(dist).to_dict()
# Chhange laytout positions of the graph
pos = nx.kamada_kawai_layout(G, dist=distance)
# Draw nodes and labels
nx.draw_networkx_nodes(G, pos, node_color='b', alpha=0.5)
nx.draw_networkx_labels(G, pos, labels=label_dict, font_color='w')
Let me know what you think! I like it when dealing with high dimensional vectors.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Feature request: plot graph to visualise distance #9 - GitHub
The networkx implementation of the kamada kawai graph layout allows you to input a dictionary containing the distance of each node to the...
Read more >Python Scatter Plot - How to visualize relationship between ...
Scatter plot is a graph of two sets of data along the two axes. It is used to visualize the relationship between the...
Read more >Proximity analysis—Help | ArcGIS for Desktop
The Near tool calculates the distance from each point in one feature class to the nearest point or line feature in another feature...
Read more >A Complete Guide to Line Charts | Tutorial by Chartio
Line charts are a fundamental chart type generally used to show change in values across time. Learn how to best use this chart...
Read more >QGIS Select features with a specific distance - YouTube
In this video I will show you how to select features from a point layer that are within 10km of a study area.QGIS...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Literarily said so in the second sentence haha, so yes. Expect I don’t draw the edges, just the nodes. You could but then they would all just be connected to each other, which doesn’t add much to the visual.
wrt your last comment, I’ll see if I have some time tonight to do that.
this went in.