Skip to content

Instantly share code, notes, and snippets.

@mikk-c
Last active November 26, 2024 09:17
Show Gist options
  • Select an option

  • Save mikk-c/c216ec8fc40ddbf1222519c6b1db82e4 to your computer and use it in GitHub Desktop.

Select an option

Save mikk-c/c216ec8fc40ddbf1222519c6b1db82e4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "381562c4-64de-4a58-acaf-fab66a38e26f",
"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": "e8b7c215-4164-4048-90ec-00156dacbf24",
"metadata": {},
"outputs": [],
"source": [
"def loss(emb, A):\n",
" l = 0\n",
" for u in range(A.shape[0]):\n",
" for v in range(A.shape[0]):\n",
" l += (np.linalg.norm(emb[u] - emb[v]) ** 2) * A[u,v]\n",
" return l / 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "108e3bb1-010c-4a28-ba92-47fa803ffe73",
"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": 4,
"id": "d99f3813-48e2-45d3-9415-8dc57376ca25",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-0.20831658, -0.28388127],\n",
" [-0.14119158, -0.19244675],\n",
" [-0.30732743, -0.28644885],\n",
" [-0.23195072, -0.20727592],\n",
" [-0.28108414, -0.32997234],\n",
" [-0.23352491, -0.27090093],\n",
" [-0.25762119, -0.33830885],\n",
" [-0.17767727, -0.15268286],\n",
" [-0.37952009, 0.3508772 ],\n",
" [-0.24939078, 0.19509741],\n",
" [-0.36128451, 0.30608578],\n",
" [-0.20868972, 0.19763654],\n",
" [-0.20868972, 0.19763654],\n",
" [-0.10382218, 0.08737961],\n",
" [-0.34971158, 0.31097189]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D = np.array(list(dict(G.degree).values())) ** (-1/2)\n",
"A = nx.adjacency_matrix(G).todense()\n",
"L = D * A * D\n",
"U, s, Vt = svd(L)\n",
"embeddings_1 = U[:,:2]\n",
"\n",
"embeddings_1"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "06a296b3-000b-46b1-99aa-a44557d9dd91",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.26479243, -0.23687981],\n",
" [-0.20096649, -0.19758656],\n",
" [ 0.42348481, -0.19945908],\n",
" [-0.36246555, -0.16107308],\n",
" [ 0.00189407, -0.21310377],\n",
" [-0.04132578, -0.25978185],\n",
" [-0.13225023, -0.26626225],\n",
" [-0.08072732, -0.12716786],\n",
" [ 0.04347252, 0.33342402],\n",
" [-0.35983281, 0.30059236],\n",
" [ 0.33667111, 0.29722324],\n",
" [-0.12140961, 0.33599228],\n",
" [-0.12140961, 0.33599228],\n",
" [-0.294756 , 0.198633 ],\n",
" [ 0.4381549 , 0.28990733]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"I = np.eye(A.shape[0])\n",
"L = (I - A).T.dot(I - A)\n",
"vals, vects = np.linalg.eig(L)\n",
"embeddings_2 = vects[:,1:3]\n",
"\n",
"embeddings_2"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "bb647748-edce-4268-8dda-c6e76b975977",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.22633937, 0.26347611],\n",
" [ 0.18107149, 0.21160983],\n",
" [ 0.31687511, 0.24404261],\n",
" [ 0.27160724, 0.18518209],\n",
" [ 0.31687511, 0.26700862],\n",
" [ 0.27160724, 0.28217736],\n",
" [ 0.27160724, 0.30420962],\n",
" [ 0.22633937, 0.1398174 ],\n",
" [ 0.31687511, -0.32528693],\n",
" [ 0.27160724, -0.26498289],\n",
" [ 0.27160724, -0.28141323],\n",
" [ 0.22633937, -0.29016645],\n",
" [ 0.22633937, -0.29016645],\n",
" [ 0.13580362, -0.16757346],\n",
" [ 0.27160724, -0.27793422]])"
]
},
"execution_count": 6,
"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_3 = vects[:,:2]\n",
"\n",
"embeddings_3"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "5d46d6b1-5628-4109-a728-d5033fffee84",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.2433272640711013\n",
"9.577299426088956\n",
"1.3720145939363744\n"
]
}
],
"source": [
"print(loss(embeddings_1, A))\n",
"print(loss(embeddings_2, A))\n",
"print(loss(embeddings_3, A))"
]
}
],
"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