{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":0: FutureWarning: IPython widgets are experimental and may change in the future.\n" ] } ], "source": [ "%run \"libraries.ipynb\"\n", "\n", "from IPython.display import display, HTML\n", "from IPython.html import widgets\n", "import matplotlib.patheffects as path_effects" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "hyperlinks_graph = nx.read_gexf(\"data/hyperlinks.gexf\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "g = nx.DiGraph()\n", "\n", "nb_out = 3\n", "\n", "hyperlinks_graph.remove_nodes_from([ \"Geometry\", \"Mathematics\" ])\n", "\n", "for u in hyperlinks_graph:\n", " nb = sorted(hyperlinks_graph[u].items(), key=lambda x: -x[1][\"term occurence\"])\n", " \n", " # filtering self loops\n", " nb = [ n for n in nb if n[0] != u ]\n", "\n", " for v in nb[0:nb_out]:\n", " #print v\n", " g.add_edge(u, v[0], attr_dict=v[1])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "reduced graph\n", "=============\n", "nodes: 296\n", "edges: 838\n", "reduction: 838/3469 (0.241568175267)\n" ] } ], "source": [ "print \"reduced graph\"\n", "print \"=============\"\n", "print \"nodes: %s\" % (len(g.nodes()))\n", "print \"edges: %s\" % (len(g.edges()))\n", "print \"reduction: %s/%s (%s)\" % (len(g.edges()),\n", " len(hyperlinks_graph.edges()),\n", " float(len(g.edges()))/float(len(hyperlinks_graph.edges())))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nx.write_gexf(g, \"data/reading_maps/pages-hyperlinks-reduced-3.gexf\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## community detection" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import community\n", "partitions = community.best_partition(g.to_undirected())" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
group %s | \" % (c)\n", " html += \", \".join(map(lambda x: u\"{0}\".format(x), ps))\n", " html += \" |