|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import matplotlib.pyplot as plt\n", |
| 10 | + "\n", |
| 11 | + "def draw_graph(G, node_names={}, node_size=500):\n", |
| 12 | + " pos_nodes = nx.spring_layout(G)\n", |
| 13 | + " nx.draw(G, pos_nodes, with_labels=True, node_size=node_size, edge_color='gray', arrowsize=30)\n", |
| 14 | + " \n", |
| 15 | + " pos_attrs = {}\n", |
| 16 | + " for node, coords in pos_nodes.items():\n", |
| 17 | + " pos_attrs[node] = (coords[0], coords[1] + 0.08)\n", |
| 18 | + " \n", |
| 19 | + " #nx.draw_networkx_labels(G, pos_attrs, font_family='serif', font_size=20)\n", |
| 20 | + " \n", |
| 21 | + " plt.axis('off')\n", |
| 22 | + " axis = plt.gca()\n", |
| 23 | + " axis.set_xlim([1.2*x for x in axis.get_xlim()])\n", |
| 24 | + " axis.set_ylim([1.2*y for y in axis.get_ylim()])\n", |
| 25 | + " plt.show()" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "## Graph Factorization" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "import networkx as nx\n", |
| 42 | + "\n", |
| 43 | + "G = nx.barbell_graph(m1=3, m2=2)\n", |
| 44 | + "draw_graph(G)\n" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "from gem.embedding.gf import GraphFactorization\n", |
| 54 | + "\n", |
| 55 | + "G = nx.barbell_graph(m1=10, m2=4)\n", |
| 56 | + "draw_graph(G)\n", |
| 57 | + "\n", |
| 58 | + "gf = GraphFactorization(d=2, data_set=None,max_iter=10000, eta=1*10**-4, regu=1.0)\n", |
| 59 | + "gf.learn_embedding(G)" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "import matplotlib.pyplot as plt\n", |
| 69 | + "\n", |
| 70 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 71 | + "\n", |
| 72 | + "for x in G.nodes():\n", |
| 73 | + " \n", |
| 74 | + " v = gf.get_embedding()[x]\n", |
| 75 | + " ax.scatter(v[0],v[1], s=1000)\n", |
| 76 | + " ax.annotate(str(x), (v[0],v[1]), fontsize=12)" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "## GraphRep" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "import networkx as nx\n", |
| 93 | + "from karateclub.node_embedding.neighbourhood.grarep import GraRep\n", |
| 94 | + "\n", |
| 95 | + "G = nx.barbell_graph(m1=10, m2=4)\n", |
| 96 | + "draw_graph(G)\n", |
| 97 | + "\n", |
| 98 | + "gr = GraRep(dimensions=2,order=3)\n", |
| 99 | + "gr.fit(G)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "import matplotlib.pyplot as plt\n", |
| 109 | + "\n", |
| 110 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 111 | + "\n", |
| 112 | + "ida = 4\n", |
| 113 | + "idb = 5\n", |
| 114 | + "for x in G.nodes():\n", |
| 115 | + " \n", |
| 116 | + " v = gr.get_embedding()[x]\n", |
| 117 | + " ax.scatter(v[ida],v[idb], s=1000)\n", |
| 118 | + " ax.annotate(str(x), (v[ida],v[idb]), fontsize=12)" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "metadata": {}, |
| 124 | + "source": [ |
| 125 | + "## HOPE" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "import networkx as nx\n", |
| 135 | + "from gem.embedding.hope import HOPE\n", |
| 136 | + "\n", |
| 137 | + "G = nx.barbell_graph(m1=10, m2=4)\n", |
| 138 | + "draw_graph(G)\n", |
| 139 | + "\n", |
| 140 | + "hp = HOPE(d=4, beta=0.01)\n", |
| 141 | + "hp.learn_embedding(G)" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": null, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "import matplotlib.pyplot as plt\n", |
| 151 | + "\n", |
| 152 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 153 | + "\n", |
| 154 | + "for x in G.nodes():\n", |
| 155 | + " \n", |
| 156 | + " v = hp.get_embedding()[x,2:]\n", |
| 157 | + " ax.scatter(v[0],v[1], s=1000)\n", |
| 158 | + " ax.annotate(str(x), (v[0],v[1]), fontsize=20)" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "markdown", |
| 163 | + "metadata": {}, |
| 164 | + "source": [ |
| 165 | + "## DeepWalk" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": null, |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [], |
| 173 | + "source": [ |
| 174 | + "import networkx as nx\n", |
| 175 | + "from karateclub.node_embedding.neighbourhood.deepwalk import DeepWalk\n", |
| 176 | + "\n", |
| 177 | + "G = nx.barbell_graph(m1=10, m2=4)\n", |
| 178 | + "draw_graph(G)\n", |
| 179 | + "\n", |
| 180 | + "dw = DeepWalk(dimensions=2)\n", |
| 181 | + "dw.fit(G)" |
| 182 | + ] |
| 183 | + }, |
| 184 | + { |
| 185 | + "cell_type": "code", |
| 186 | + "execution_count": null, |
| 187 | + "metadata": {}, |
| 188 | + "outputs": [], |
| 189 | + "source": [ |
| 190 | + "import matplotlib.pyplot as plt\n", |
| 191 | + "\n", |
| 192 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 193 | + "\n", |
| 194 | + "for x in G.nodes():\n", |
| 195 | + " \n", |
| 196 | + " v = dw.get_embedding()[x]\n", |
| 197 | + " ax.scatter(v[0],v[1], s=1000)\n", |
| 198 | + " ax.annotate(str(x), (v[0],v[1]), fontsize=12)" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "markdown", |
| 203 | + "metadata": {}, |
| 204 | + "source": [ |
| 205 | + "## Node2Vec" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "code", |
| 210 | + "execution_count": null, |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + "import networkx as nx\n", |
| 215 | + "from node2vec import Node2Vec\n", |
| 216 | + "\n", |
| 217 | + "G = nx.barbell_graph(m1=10, m2=4)\n", |
| 218 | + "draw_graph(G)\n", |
| 219 | + "\n", |
| 220 | + "node2vec = Node2Vec(G, dimensions=2)\n", |
| 221 | + "model = node2vec.fit(window=10)\n", |
| 222 | + "embeddings = model.wv" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": null, |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [], |
| 230 | + "source": [ |
| 231 | + "import matplotlib.pyplot as plt\n", |
| 232 | + "\n", |
| 233 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 234 | + "\n", |
| 235 | + "for x in G.nodes():\n", |
| 236 | + " \n", |
| 237 | + " v = model.wv[str(x)]\n", |
| 238 | + " ax.scatter(v[0],v[1], s=1000)\n", |
| 239 | + " ax.annotate(str(x), (v[0],v[1]), fontsize=16)\n", |
| 240 | + "\n", |
| 241 | + "plt.show()" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "markdown", |
| 246 | + "metadata": {}, |
| 247 | + "source": [ |
| 248 | + "## Edge2Vec" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "code", |
| 253 | + "execution_count": null, |
| 254 | + "metadata": {}, |
| 255 | + "outputs": [], |
| 256 | + "source": [ |
| 257 | + "from node2vec.edges import HadamardEmbedder\n", |
| 258 | + "edges_embs = HadamardEmbedder(keyed_vectors=model.wv)" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": null, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [], |
| 266 | + "source": [ |
| 267 | + "import matplotlib.pyplot as plt\n", |
| 268 | + "\n", |
| 269 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 270 | + "\n", |
| 271 | + "for x in G.edges():\n", |
| 272 | + " \n", |
| 273 | + " v = edges_embs[(str(x[0]), str(x[1]))]\n", |
| 274 | + " ax.scatter(v[0],v[1], s=1000)\n", |
| 275 | + " ax.annotate(str(x), (v[0],v[1]), fontsize=16)\n", |
| 276 | + "\n", |
| 277 | + "plt.show()" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "markdown", |
| 282 | + "metadata": {}, |
| 283 | + "source": [ |
| 284 | + "## Graph2Vec" |
| 285 | + ] |
| 286 | + }, |
| 287 | + { |
| 288 | + "cell_type": "code", |
| 289 | + "execution_count": null, |
| 290 | + "metadata": {}, |
| 291 | + "outputs": [], |
| 292 | + "source": [ |
| 293 | + "import random\n", |
| 294 | + "import matplotlib.pyplot as plt\n", |
| 295 | + "from karateclub import Graph2Vec\n", |
| 296 | + "\n", |
| 297 | + "n_graphs = 20\n", |
| 298 | + "\n", |
| 299 | + "def generate_radom():\n", |
| 300 | + " n = random.randint(6, 20)\n", |
| 301 | + " k = random.randint(5, n)\n", |
| 302 | + " p = random.uniform(0, 1)\n", |
| 303 | + " return nx.watts_strogatz_graph(n,k,p), [n,k,p]\n", |
| 304 | + "\n", |
| 305 | + "Gs = [generate_radom() for x in range(n_graphs)]\n", |
| 306 | + "\n", |
| 307 | + "model = Graph2Vec(dimensions=2, wl_iterations=10)\n", |
| 308 | + "model.fit([x[0] for x in Gs])\n", |
| 309 | + "embeddings = model.get_embedding()\n", |
| 310 | + "\n", |
| 311 | + "fig, ax = plt.subplots(figsize=(10,10))\n", |
| 312 | + "\n", |
| 313 | + "for i,vec in enumerate(embeddings):\n", |
| 314 | + " \n", |
| 315 | + " ax.scatter(vec[0],vec[1], s=1000)\n", |
| 316 | + " ax.annotate(str(i), (vec[0],vec[1]), fontsize=40)" |
| 317 | + ] |
| 318 | + }, |
| 319 | + { |
| 320 | + "cell_type": "code", |
| 321 | + "execution_count": null, |
| 322 | + "metadata": {}, |
| 323 | + "outputs": [], |
| 324 | + "source": [] |
| 325 | + } |
| 326 | + ], |
| 327 | + "metadata": { |
| 328 | + "kernelspec": { |
| 329 | + "display_name": "Python 3", |
| 330 | + "language": "python", |
| 331 | + "name": "python3" |
| 332 | + }, |
| 333 | + "language_info": { |
| 334 | + "codemirror_mode": { |
| 335 | + "name": "ipython", |
| 336 | + "version": 3 |
| 337 | + }, |
| 338 | + "file_extension": ".py", |
| 339 | + "mimetype": "text/x-python", |
| 340 | + "name": "python", |
| 341 | + "nbconvert_exporter": "python", |
| 342 | + "pygments_lexer": "ipython3", |
| 343 | + "version": "3.6.8" |
| 344 | + } |
| 345 | + }, |
| 346 | + "nbformat": 4, |
| 347 | + "nbformat_minor": 4 |
| 348 | +} |
0 commit comments