{ "metadata": { "name": "", "signature": "sha256:6b316326ba3cf7e552e2ac2942b236727024333a84f4476b87eecbff24911ae3" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Graph Analysis - I" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Imports" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The new import we are doing in this class is networkx:\n", "\n", "http://networkx.github.io/documentation/latest/tutorial/" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "\n", "import scipy as sp\n", "import scipy.sparse.linalg as linalg\n", "import scipy.cluster.hierarchy as hr\n", "from scipy.spatial.distance import pdist, squareform\n", "\n", "import sklearn.datasets as datasets\n", "import sklearn.metrics as metrics\n", "import sklearn.utils as utils\n", "import sklearn.linear_model as linear_model\n", "import sklearn.svm as svm\n", "import sklearn.cross_validation as cross_validation\n", "import sklearn.cluster as cluster\n", "from sklearn.ensemble import AdaBoostClassifier\n", "from sklearn.neighbors import KNeighborsClassifier\n", "from sklearn.decomposition import TruncatedSVD\n", "from sklearn.preprocessing import StandardScaler\n", "\n", "import statsmodels.api as sm\n", "\n", "from patsy import dmatrices\n", "\n", "import networkx as nx\n", "\n", "import seaborn as sns\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic graph concepts in NetworkX" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Undirected Graphs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g = nx.Graph()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding to the graph one node at a time" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.add_node(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding multiple nodes at a time" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.add_nodes_from([2,3])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nodes are objects themselves" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.add_node('ET')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.nodes()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nodes can also be removed" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.remove_node(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.nodes()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Adding edges to the graph" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.add_edge(1,2)\n", "g.add_edge(3,'ET')\n", "g.add_edges_from([(2,3), (1,3)])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.edges()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.nodes()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Removing edges" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.remove_edge(1,2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.edges()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.nodes()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Neighbors, degrees etc." ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.neighbors(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "g.degree(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "Any NetworkX graph behaves like a Python dictionary with nodes as primary keys" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.add_node(1, time='5pm')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "g.node[1]['time']" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "g.node[1] # Python dictionary" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The special edge attribute \"weight\" should always be numeric and holds values used by\n", "algorithms requiring weighted edges. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "g.add_edge(1, 2, weight=4.0 )" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "g[1][2]['weight'] = 5.0 # edge already added " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "g[1][2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "Node and edge iterators" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for node in g:\n", " print 'nodeid: ', node, '\\t degree:', g.degree(node)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print g.edges(data=True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Directed graphs: \n", "\n", "http://networkx.lanl.gov/reference/classes.digraph.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "G = nx.DiGraph()\n", "G.add_node(1)\n", "G.add_nodes_from([2,3])\n", "G.add_nodes_from(range(100,110))\n", "H=nx.Graph()\n", "H.add_path([0,1,2,3,4,5,6,7,8,9])\n", "G.add_nodes_from(H)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print G.nodes()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "G can also grow by adding edges" ] }, { "cell_type": "code", "collapsed": false, "input": [ "G.add_edge(1, 2)\n", "G.add_edges_from([(1,2),(1,3)])\n", "G.add_edges_from(H.edges())" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print G.edges()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Attributes:\n", "\n", "Each graph, node, and edge can hold key/value attribute pairs in an associated attribute dictionary (the keys must be hashable). By default these are empty, but can be added or changed using add_edge, add_node or direct manipulation of the attribute dictionaries named graph, node and edge respectively." ] }, { "cell_type": "code", "collapsed": false, "input": [ "G = nx.DiGraph(day=\"Friday\")\n", "print G.graph" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add node attributes using add_node(), add_nodes_from() or G.node" ] }, { "cell_type": "code", "collapsed": false, "input": [ "G.add_node(1, time='5pm')\n", "G.add_nodes_from([3], time='2pm')\n", "print G.node[1]\n", "G.node[1]['room'] = 714\n", "del G.node[1]['room'] # remove attribute\n", "print G.nodes(data=True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add edge attributes using add_edge(), add_edges_from(), subscript notation, or G.edge." ] }, { "cell_type": "code", "collapsed": false, "input": [ "G.add_edge(1, 2, weight=4.7 )\n", "G.add_edges_from([(3,4),(4,5)], color='red')\n", "G.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})])\n", "G[1][2]['weight'] = 4.7\n", "G.edge[1][2]['weight'] = 4\n", "\n", "print G.edges(data=True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many common graph features allow python syntax to speed reporting." ] }, { "cell_type": "code", "collapsed": false, "input": [ "1 in G # check if node in graph" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "[n for n in G if n<3] # iterate through nodes" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "len(G) # number of nodes in graph" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print G[1] # adjacency dict keyed by neighbor to edge attributes\n", "... # Note: you should not change this dict manually!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Iterating over the edges of a graph" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for n,nbrsdict in G.adjacency_iter():\n", " for nbr,eattr in nbrsdict.items():\n", " if 'weight' in eattr:\n", " print (n,nbr,eattr['weight'])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or" ] }, { "cell_type": "code", "collapsed": false, "input": [ "[ (u,v,edata['weight']) for u,v,edata in G.edges(data=True) if 'weight' in edata ]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Reading graphs from files:\n", "\n", "http://networkx.lanl.gov/reference/readwrite.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reading .gml files" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Ggml = nx.read_gml('polblogs.gml')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print len(Ggml.nodes())\n", "print len(Ggml.edges())" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "with sns.axes_style('white'):\n", " fig = plt.subplots(1, figsize=(12,8))\n", " nx.draw_networkx(Ggml, edge_color='#a4a4a4', node_size=50, with_labels=False, arrows=False)\n", " plt.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reading graphs as edge lists" ] }, { "cell_type": "code", "collapsed": false, "input": [ "with open('football.txt', 'r') as f:\n", " football = nx.read_edgelist(f, comments='#', nodetype=int, data=False)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print len(football.nodes())\n", "print len(football.edges())" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "with sns.axes_style('white'):\n", " fig = plt.subplots(1, figsize=(12,8))\n", " nx.draw_networkx(football, edge_color='#a4a4a4', node_size=50, with_labels=False)\n", " plt.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Generating graphs using the routines already available in python (for small data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Networkx has a wealth of data-generation routines that can be found here:\n", " \n", " https://networkx.github.io/documentation/latest/reference/generators.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the function that generates the Zachary's Karate club network data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "kn=nx.karate_club_graph()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "num_nodes = kn.number_of_nodes()\n", "print 'number of nodes: ' + str(num_nodes)\n", "num_edges = kn.number_of_edges()\n", "print 'number of edges: ' + str(num_edges)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Drawing the network" ] }, { "cell_type": "code", "collapsed": false, "input": [ "with sns.axes_style('white'):\n", " fig = plt.subplots(1, figsize=(12,8))\n", " nx.draw_networkx(kn, edge_color='#a4a4a4', with_labels=True, font_color='#cacaca')\n", " plt.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fl = nx.florentine_families_graph()\n", "num_nodes = fl.number_of_nodes()\n", "print 'number of nodes: ' + str(num_nodes)\n", "num_edges = fl.number_of_edges()\n", "print 'number of edges: ' + str(num_edges)\n", "with sns.axes_style('white'):\n", " fig = plt.subplots(1, figsize=(12,8))\n", " nx.draw_networkx(fl, edge_color='#a4a4a4', node_size=0, with_labels=True)\n", " plt.axis('off')\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Experimenting with different graph models" ] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Experimenting with Erdos-Renyi random graphs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "er=nx.erdos_renyi_graph(1000,0.15)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print type(er)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Number of nodes in the random graph: \", er.number_of_nodes() \n", "print \"Number of edges in the random graph: \", er.number_of_edges() \n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "with sns.axes_style('white'):\n", " fig = plt.subplots(1, figsize=(12,8))\n", " nx.draw_networkx(er, node_size=15, edge_color='#a4a4a4', with_labels=False, alpha=.4, linewidths=0)\n", " plt.axis('off')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Degree distribution" ] }, { "cell_type": "code", "collapsed": false, "input": [ "degree_sequence=sorted(nx.degree(er).values(),reverse=True) \n", "dmax=max(degree_sequence)\n", "print dmax" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "h,bins,patches = plt.hist(degree_sequence,bins=dmax)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "hmax=max(h)\n", "plt.axis([1,dmax,1,hmax]) # set ranges\n", "#x=compress(h,bins) # remove bins with zero entries\n", "#y=compress(h,h) # remove corresponding entries\n", "x=bins.compress(h)\n", "y=h.compress(h)\n", "plt.plot(x,y,'bo')\n", "plt.title(\"Degree distribution\")\n", "plt.xlabel(\"degree\")\n", "plt.ylabel(\"number of nodes\")\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Connected Components" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two nodes of a graph belong in the same connected component if there is a path of edges of the graph that connects these two nodes." ] }, { "cell_type": "code", "collapsed": false, "input": [ "cc= nx.connected_components(er)\n", "print type(cc)\n", "print [len(s) for s in cc]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def print_cc_sizes(g):\n", " cc = nx.connected_components(g)\n", " print [len(s) for s in cc]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Clustering coefficient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **clustering coefficient of a node** is defined as the number of possible triangles centered in this node, divided by the total number of possible triangles in which this node can participate in. Formally, the clustering coefficient of a node $u$ is defined as $$c_u=\\frac{2T(u)}{d(u)(d(u)-1)},$$\n", "where $T(u)$ is the number of triangles through node $u$ and $d(u)$ is the degree of node $u$.\n", "\n", "For more details for weighted graphs etc see: \n", "\n", "http://networkx.lanl.gov/reference/generated/networkx.algorithms.cluster.clustering.html#networkx.algorithms.cluster.clustering\n", "\n", "The **average clustering coefficient** is the average clustering coefficient of all the nodes in the graph.\n", "\n", "http://networkx.lanl.gov/reference/generated/networkx.algorithms.cluster.average_clustering.html#networkx.algorithms.cluster.average_clustering" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ccall = nx.clustering(er)\n", "clustering_coefficient = nx.average_clustering(er)\n", "\n", "print clustering_coefficient" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "h,bins,patches = plt.hist(list(nx.clustering(er).values()))\n", "plt.title('clustering coefficients')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Triangles" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(nx.triangles(er,0))\n", "#print(nx.triangles(er))\n", "h,bins, patches = plt.hist(list(nx.triangles(er).values()))\n", "plt.title('Triangles')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Diameter and average path length" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **diameter** of a graph is defined as the largest shortest path between any two nodes in the graph" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(nx.diameter(er))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **average shortest path length** of a graph is defined as the average of all shortest path lengths in the graph\n", "\n", "http://networkx.lanl.gov/reference/generated/networkx.algorithms.shortest_paths.generic.average_shortest_path_length.html" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(nx.average_shortest_path_length(er))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Experimenting with Watts-Strogatz graphs\n", "\n", "http://networkx.lanl.gov/reference/generated/networkx.generators.random_graphs.watts_strogatz_graph.html" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ws=nx.watts_strogatz_graph(500,5,0.1)\n", "print_cc_sizes(ws)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Degree distribution" ] }, { "cell_type": "code", "collapsed": false, "input": [ "degree_sequence=sorted(nx.degree(ws).values(),reverse=True) \n", "dmax=max(degree_sequence)\n", "print dmax" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "h,bins,patches = plt.hist(degree_sequence,bins=dmax)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "hmax=max(h)\n", "plt.axis([1,dmax,1,hmax]) # set ranges\n", "#x=compress(h,bins) # remove bins with zero entries\n", "#y=compress(h,h) # remove corresponding entries\n", "x=bins.compress(h)\n", "y=h.compress(h)\n", "plt.plot(x,y,'bo')\n", "plt.title(\"Degree distribution\")\n", "plt.xlabel(\"degree\")\n", "plt.ylabel(\"number of nodes\")\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clustering coefficient" ] }, { "cell_type": "code", "collapsed": false, "input": [ "h,bins,patches = plt.hist(list(nx.clustering(ws).values()))\n", "plt.title('clustering coefficients')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Average path length and diameter" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 'Diameter:', (nx.diameter(ws))\n", "print 'Average shortest path length:', (nx.average_shortest_path_length(ws))\n", "print 'Average clustering coefficient:', (nx.average_clustering(ws))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "r = range(4,7)\n", "d = np.zeros(len(r))\n", "cc = np.zeros(len(r))\n", "pl = np.zeros(len(r))\n", "index = 0\n", "for i in r:\n", " ws=nx.watts_strogatz_graph(500,i,0.1)\n", " d[index] = nx.diameter(ws)\n", " cc[index] = nx.average_clustering(ws)\n", " pl[index] = nx.average_shortest_path_length(ws)\n", " index=+1\n", "plt.plot(r,d,'r')\n", "plt.plot(r,cc,'b')\n", "#plt.plot(r,pl,'g');" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Experimenting with Barabasi-Albert graphs\n", "\n", "http://networkx.lanl.gov/reference/generated/networkx.generators.random_graphs.barabasi_albert_graph.html" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ba=nx.barabasi_albert_graph(500,5)\n", "print_cc_sizes(ba)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Degree distribution" ] }, { "cell_type": "code", "collapsed": false, "input": [ "degree_sequence=sorted(nx.degree(ba).values(),reverse=True) \n", "dmax=max(degree_sequence)\n", "print dmax" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "h,bins,patches = plt.hist(degree_sequence,bins=dmax)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "hmax=max(h)\n", "plt.axis([1,dmax,1,hmax]) # set ranges\n", "#x=compress(h,bins) # remove bins with zero entries\n", "#y=compress(h,h) # remove corresponding entries\n", "x=bins.compress(h)\n", "y=h.compress(h)\n", "plt.loglog(x,y,'bo')\n", "plt.title(\"Degree distribution\")\n", "plt.xlabel(\"degree\")\n", "plt.ylabel(\"number of nodes\")\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Code for setting the style of the notebook\n", "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"../theme/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }