Created
September 4, 2020 07:59
-
-
Save mikk-c/0dff575f31d8bc745a9914e7ac4df4e1 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 copy\n", | |
"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 lapl_read(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\n", | |
"\n", | |
"def spl_read(filename):\n", | |
" v = {}\n", | |
" with open(filename, 'r') as f:\n", | |
" for line in f:\n", | |
" fields = line.strip().split('\\t')\n", | |
" value = float(fields[1])\n", | |
" if value > 0: \n", | |
" v[int(fields[0])] = value\n", | |
" v = {x: v[x] / sum(v.values()) for x in v}\n", | |
" return v" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"G = nx.read_edgelist(\"data.txt\", delimiter = \"\\t\", nodetype = int)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Laplacian distance: 0.09683957477939774\n" | |
] | |
} | |
], | |
"source": [ | |
"v1_lapl = lapl_read(\"../1/vector1.txt\")\n", | |
"v2_lapl = lapl_read(\"../1/vector2.txt\")\n", | |
"A = nx.adjacency_matrix(G).todense().astype(float)\n", | |
"L = np.linalg.pinv(csgraph.laplacian(np.matrix(A), normed = False))\n", | |
"diff = v1_lapl - v2_lapl\n", | |
"print(\"Laplacian distance: %s\" % np.sqrt(diff.T.dot(np.array(L).dot(diff))))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"SPL distance: 2.932696524931615\n" | |
] | |
} | |
], | |
"source": [ | |
"v1_spl = spl_read(\"../1/vector1.txt\")\n", | |
"v2_spl = spl_read(\"../1/vector2.txt\")\n", | |
"spls = dict(nx.shortest_path_length(G))\n", | |
"print(\"SPL distance: %s\" % sum(v1_spl[origin] * v2_spl[dest] * spls[dest][origin] for origin in v1_spl for dest in v2_spl))" | |
] | |
} | |
], | |
"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