question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: plot graph to visualise distance

See original GitHub issue

I’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:

image

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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Arsillacommented, Mar 9, 2020

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.

0reactions
koaningcommented, Mar 20, 2020

this went in.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found