Created
August 4, 2020 16:26
-
-
Save imohitmayank/2d8032ec397eb06481052d003604f0ae to your computer and use it in GitHub Desktop.
To test the node2vec model of KarateClub on the LesMiserable graph (as reported in paper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3-final" | |
}, | |
"orig_nbformat": 2, | |
"kernelspec": { | |
"name": "python_defaultSpec_1596558154926", | |
"display_name": "Python 3.7.3 64-bit ('base': conda)" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "<IPython.lib.display.IFrame at 0x1842b43ffd0>", | |
"text/html": "\n <iframe\n width=\"500px\"\n height=\"500px\"\n src=\"nx.html\"\n frameborder=\"0\"\n allowfullscreen\n ></iframe>\n " | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
], | |
"source": [ | |
"\"\"\"\n", | |
"Author: Mohit Mayank\n", | |
"Idea: To test the node2vec model of KarateClub on the LesMiserable graph (as reported in paper)\n", | |
"Tests: Run the test for two setting, \n", | |
" 1. To test Homophily, run with p=1, q=0.5 and n_clusters=6\n", | |
" 2. To test Structural equivalenc, run with p=1, q=2 and n_clusters=3\n", | |
"Observation: Test 1 gives respectable result with community marked with same cluster. \n", | |
" Test 2 result not able to replicate as other parameters like walk_length and num_walks is not mentioned in paper.\n", | |
"\n", | |
"\n", | |
"Node2Vec Paper: https://cs.stanford.edu/~jure/pubs/node2vec-kdd16.pdf\n", | |
"\"\"\"\n", | |
"# import\n", | |
"import networkx as nx\n", | |
"from karateclub import Node2Vec\n", | |
"from sklearn.cluster import KMeans\n", | |
"from pyvis.network import Network\n", | |
"\n", | |
"# functions\n", | |
"def plot_graph(graph):\n", | |
" nt = Network(\"500px\", \"500px\", notebook=True)\n", | |
" nt.from_nx(graph)\n", | |
" return nt.show(\"nx.html\")\n", | |
"\n", | |
"# get data\n", | |
"G = nx.generators.social.les_miserables_graph()\n", | |
"# mapping for node label\n", | |
"mapping = {}\n", | |
"for i, node in enumerate(G.nodes()):\n", | |
" mapping[node] = i\n", | |
"H = nx.relabel_nodes(G, mapping)\n", | |
"# run node2vec \n", | |
"model = Node2Vec(p=1, q=2)\n", | |
"model.fit(H)\n", | |
"# perform clustering\n", | |
"kmeans = KMeans(n_clusters=3, random_state=0).fit(model.get_embedding())\n", | |
"# color the nodes\n", | |
"colors = [\"#46019b\", \"#007efe\", \"#00bb00\", \"#fef601\", \"#dd0000\", \"#232345\"]\n", | |
"for i, x in enumerate(kmeans.labels_):\n", | |
" H.nodes[i]['color'] = colors[x]\n", | |
"# plot the network\n", | |
"plot_graph(H)" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment