Created
September 1, 2020 13:50
-
-
Save mikk-c/261bcdce5387248cf1c381fcbb2bd687 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, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import networkx as nx\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "from sklearn.cluster import KMeans\n", | |
| "from sklearn.manifold import TSNE\n", | |
| "from sklearn.metrics import normalized_mutual_info_score" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "G = nx.read_edgelist(\"1/data.txt\", create_using = nx.Graph(), delimiter = \"\\t\")\n", | |
| "nodes = list(G.nodes)\n", | |
| "A = nx.to_numpy_array(G, nodelist = nodes)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "ground_truth = {}\n", | |
| "with open(\"1/nodes.txt\", 'r') as f:\n", | |
| " for line in f:\n", | |
| " fields = line.strip().split('\\t')\n", | |
| " ground_truth[fields[0]] = int(fields[1])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "A_embedded = TSNE(n_components = 3).fit_transform(A)\n", | |
| "kmeans = KMeans(n_clusters = 8).fit(A_embedded)\n", | |
| "ground_truth = [ground_truth[n] for n in nodes]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "lp = list(nx.algorithms.community.asyn_lpa_communities(G))\n", | |
| "lp = {n: c for c in range(len(lp)) for n in lp[c]}\n", | |
| "lp_array = []\n", | |
| "for n in G.nodes:\n", | |
| " lp_array.append(lp[n])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0.2781393636303484\n", | |
| "0.5402861976540427\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print(normalized_mutual_info_score(kmeans.labels_, ground_truth))\n", | |
| "print(normalized_mutual_info_score(lp_array, ground_truth))" | |
| ] | |
| } | |
| ], | |
| "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