|
From: | Teddy Yang |
Subject: | Re: [igraph] Need help to rewrite some R code to python with igraph |
Date: | Tue, 9 Apr 2013 11:33:36 +0800 |
> 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 ;)
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)
> code:
> # retrieved from: http://jean-robert.github.com/2012/01/22/cluster-your-facebook-friends.html
> ...
> g <- graph.data.frame(ga.data, directed=FALSE)
> gnc <- edge.betweenness.community(g, directed=FALSE)
gnc = g.community_edge_betweenness(directed=False)
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:
> 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
gn_groups = gnc.as_clustering().membership
> V(g)$color <- gn.groups
g.vs["color"] = gn_groups
g.vs["label"] = g.vs["name"]
> V(g)$label <- V(g)$name # this is to show the friends' name on the plot
> 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)
plot(g, "friendscluster.png")
> png('friendscluster.png')
> plot(g)
> dev.off()
--
T.
_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help
[Prev in Thread] | Current Thread | [Next in Thread] |