Skip to content

Instantly share code, notes, and snippets.

@jinhangjiang
Last active June 25, 2021 09:35
Show Gist options
  • Save jinhangjiang/b23d2caf173f046f5e8dacf50f446c7f to your computer and use it in GitHub Desktop.
Save jinhangjiang/b23d2caf173f046f5e8dacf50f446c7f to your computer and use it in GitHub Desktop.
Visualize High-Dimensional Network Data with 3D Scatter Plot
#!pip install -U node2vec
# load packages
import pandas as pd
import networkx as nx
from node2vec import Node2Vec
from matplotlib import pyplot as plt, rc, cm
from matplotlib.pyplot import figure
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
from sklearn.manifold import TSNE
# read data from github
edge = pd.read_csv("https://raw.githubusercontent.com/jinhangjiang/Datasets/main/Network%20Data/emailEUcore/email-Eu-core.txt",delimiter = " ",names=["Source","Target"])
edge["weight"] = 1 * len(edge)
label = pd.read_csv("https://raw.githubusercontent.com/jinhangjiang/Datasets/main/Network%20Data/emailEUcore/email-Eu-core-department-labels.txt",delimiter = " ",names=["Vertex","Label"])
label.Vertex = label.Vertex.astype(str)
# generate graphs
def convert_graph(edgelist,name):
graph=nx.Graph()
graph.add_weighted_edges_from([tuple(x) for x in edgelist.values])
graph.name = "Covid DisNet for" + " " + name
print(nx.info(graph))
print("------------------------------------")
print("************************************")
print("------------------------------------")
return graph
graph = convert_graph(edge, "eu email")
# calculate a starting point for vector_size for node2vec
vector_size = round(len(graph.nodes)**0.25)
# use node2vec to get embeddings
setup = Node2Vec(graph,dimensions=vector_size, walk_length=5, num_walks=5)
model = setup.fit(window=10, min_count=1)
print("--------------------------------------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment