Created
September 4, 2020 07:51
-
-
Save mikk-c/0d54309486c9ea58cd68b78472541d02 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import networkx as nx\n", | |
"from scipy.sparse import csgraph" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def read_vector(filename):\n", | |
" v = {}\n", | |
" with open(filename, 'r') as f:\n", | |
" for line in f:\n", | |
" fields = line.strip().split('\\t')\n", | |
" v[int(fields[0])] = float(fields[1])\n", | |
" v = np.array([v[n] for n in G.nodes])\n", | |
" v /= v.sum()\n", | |
" return v" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"G = nx.read_edgelist(\"data.txt\", delimiter = \"\\t\", nodetype = int)\n", | |
"\n", | |
"v1 = read_vector(\"vector1.txt\")\n", | |
"v2 = read_vector(\"vector2.txt\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"A = nx.adjacency_matrix(G).todense().astype(float)\n", | |
"L = np.linalg.pinv(csgraph.laplacian(np.matrix(A), normed = False))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.14953767659245623\n" | |
] | |
} | |
], | |
"source": [ | |
"diff = v1 - v2\n", | |
"print(np.sqrt(diff.T.dot(np.array(L).dot(diff))))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment