igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Need help to rewrite some R code to python with igraph


From: Tamás Nepusz
Subject: Re: [igraph] Need help to rewrite some R code to python with igraph
Date: Mon, 8 Apr 2013 23:51:45 +0200

> I can't find equivalent of 'community.to.membership' in the documentation of 
> igraph of python(actually, igraph.community_to_membership() exists)
That's the one you are looking for ;)

> code:
> # retrieved from: 
> http://jean-robert.github.com/2012/01/22/cluster-your-facebook-friends.html
> ...
> g <- graph.data.frame(ga.data, directed=FALSE)
This reads the graph from an R data frame; there is no equivalent in Python 
since Python does not know data frames. You have to replace it with whatever 
code you use to construct your graph (e.g., you could load the graph from a 
file using igraph.load)

> gnc <- edge.betweenness.community(g, directed=FALSE)
gnc = g.community_edge_betweenness(directed=False)

> m <- vector()
> for (s in 0:nrow(gnc$merges) ) {
>           memb <- community.to.membership(g,gnc$merge,steps=s)$membership
>           m <- c(m,modularity (g, memb, weights=NULL))
>         }
> ideal <- steps <- which(m==max(m)) - 1
> gn.groups <- community.to.membership(g,gnc$merge, steps=ideal <- 
> steps)$membership

All this is not required in the Python interface; the above code calculates the 
modularity score for each possible "cut" in the dendrogram and chooses the 
number of groups based on the maximum of the modularity function. The Python 
interface does that automatically when you convert a dendrogram into a flat 
clustering using the as_clustering() method, so all you need is:

gn_groups = gnc.as_clustering().membership

> V(g)$color <- gn.groups
g.vs["color"] = gn_groups

> V(g)$label <- V(g)$name # this is to show the friends' name on the plot       
>                                                                       
g.vs["label"] = g.vs["name"]

> V(g)$size <- 3
g.vs["size"] = 3   (well, I'd use something larger than 3; if you plot the 
graph into a PNG file, this will mean that every node has a radius of 3 pixels 
only)

> png('friendscluster.png')
> plot(g)
> dev.off()
plot(g, "friendscluster.png")

-- 
T.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]