Created
August 6, 2024 11:39
-
-
Save mikk-c/ec54ad32487a08c6222a3db2692dbbc7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "d6d6b476-e6ba-4593-83db-6b62a44ea34d", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import networkx as nx\n", | |
| "from numpy.linalg import svd" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "4b8ccb89-e1cb-428e-83c7-fd4fc9c8179e", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "H = nx.read_edgelist(\"1/data.txt\", delimiter = \"\\t\", nodetype = int)\n", | |
| "G = nx.Graph()\n", | |
| "G.add_nodes_from(sorted(H.nodes))\n", | |
| "G.add_edges_from(H.edges)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "16afde7d-2793-45c8-9664-5d207000fb9a", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[-0.26690348, -0.28863056],\n", | |
| " [-0.33362935, -0.40241527],\n", | |
| " [-0.19064534, -0.13317155],\n", | |
| " [-0.22241957, -0.13901232],\n", | |
| " [-0.19064534, -0.14331661],\n", | |
| " [-0.22241957, -0.1999696 ],\n", | |
| " [-0.22241957, -0.22055087],\n", | |
| " [-0.26690348, -0.15998305],\n", | |
| " [-0.19064534, 0.13403319],\n", | |
| " [-0.22241957, 0.13655563],\n", | |
| " [-0.22241957, 0.16338103],\n", | |
| " [-0.26690348, 0.2366512 ],\n", | |
| " [-0.26690348, 0.2366512 ],\n", | |
| " [-0.44483914, 0.61747469],\n", | |
| " [-0.22241957, 0.16230293]])" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "D = np.array(list(dict(G.degree).values())) ** (1/2)\n", | |
| "L = nx.laplacian_matrix(G).todense()\n", | |
| "L = D * L * D\n", | |
| "vals, vects = np.linalg.eig(L)\n", | |
| "embeddings = vects[:,:2]\n", | |
| "embeddings" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "df19cce6-9cd0-4fa1-96b4-84d2c1c27b4e", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "blocks = list(nx.community.label_propagation_communities(G))\n", | |
| "virtual_node_1_embedding = np.mean([embeddings[v - 1] for v in blocks[0]], axis = 0)\n", | |
| "virtual_node_2_embedding = np.mean([embeddings[v - 1] for v in blocks[1]], axis = 0)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "6bf4b469-f847-4212-9f69-c6bc5408ce8c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([-0.25093126, 0.01506295])" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "hierarchical_graph_embedding = np.mean([virtual_node_1_embedding, virtual_node_2_embedding], axis = 0)\n", | |
| "hierarchical_graph_embedding" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "433151f5-8f11-4e0b-8a7b-9645cd6beaa2", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([-2.50169057e-01, -3.75625457e-16])" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "flat_graph_embedding = np.mean(embeddings, axis = 0)\n", | |
| "flat_graph_embedding" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "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.12.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment