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: 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

Dear Tamás,

Thank you so much for your detailed reply!! I really appreciate you taking the time to respond to me.

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)
I use the adjacency matrix and igraph.Graph.Adjacency() to help me to construct the graph :)

Thank you again!


2013/4/9 Tamás Nepusz <address@hidden>
> 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.
_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help


reply via email to

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