Created
October 19, 2012 14:38
-
-
Save weaselj/3918545 to your computer and use it in GitHub Desktop.
OPA3communityTemplate for Coursera SNA class
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
# Coursera SNA optional Programming Assignment 3 template | |
# see this blog post for a nice overview of community detection algorithms | |
# http://www.r-bloggers.com/summary-of-community-detection-algorithms-in-igraph-0-6/ | |
# load the igraph library | |
# you may have to install this module if you haven't already | |
import igraph as ig | |
# read in the graph in GML format | |
# it is a sampled collection of pages from a strange set of seed categories: | |
# Math, Sociology, and Chemistry | |
# Change this to be your local file location | |
# if you are using Windows, replace the \ in the path with a double \, e.g. | |
# g = read.graph("C:\\Users\\rool\\Documents\\My Dropbox\\Education\\Social Network Analysis\\Week 3\\wikipedia.gml",format="gml") | |
g = ig.read('wikipedia.gml',format='gml') | |
ug = g.as_undirected(); | |
# obtain summary information about the graph | |
print g.summary() | |
# find the maximal k-core any vertex belongs to | |
coreness = ug.shell_index() | |
# find the largest clique using cliques(), also making sure the graph is treated as undirected | |
# fastgreedy community finding algorithm | |
fc = ug.community_fastgreedy().as_clustering() | |
# community sizes | |
print fc.sizes() | |
# membership in 30th community | |
print fc.subgraph(30).vs.get_attribute_values('label') | |
# InfoMap community finding algorithm (can be slow) | |
imc = ug.community_infomap().as_clustering() | |
# find the nodes in the largest clique | |
lc = ug.largest_cliques() | |
print ug.vs[lc[0]].get_attribute_values('label') | |
# use modularity() to find the modularity of any given partition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment