【问题标题】:extract community membership after community detection in igraph (R)在 igraph (R) 中进行社区检测后提取社区成员资格
【发布时间】:2020-01-21 03:30:24
【问题描述】:

igraph 中运行社区检测算法后,我在提取社区成员身份时遇到了一些问题。存储的membership 向量似乎与算法找到的社区成员不对应。我怀疑我遗漏了一些东西,但文档很薄,感谢任何帮助。

我的例子:

创建一个包含三个社区和两个组件的加权图:

v1 <- c(1,1,2,3,4,4,6,7,8,9)
v2 <- c(2,3,3,4,5,6,5,8,9,7)
weight <- c(3,3,2,1,3,3,2,2,2,3)

graph <- data.frame(v1,v2)
g <- graph.data.frame(graph, directed=FALSE)

上图中的社区是顶点(1,2,3),(4,5,6)和(7,8,9)。事实上,当我们运行社区检测算法时,它会发现:

cd <- fastgreedy.community(g)
plot(cd,g, edge.width=weight)
str(cd)

给出输出:

IGRAPH clustering fast greedy, groups: 3, mod: 0.56
 groups:
  1
  [1] "7" "8" "9"

  2
  [1] "4" "6" "5"

  3
  [1] "1" "2" "3"`

and this nice picture of the graph split up into communities.

但是,当我尝试访问cd$membershipcutat() 时,却是另一番景象:

cd$membership
cutat(cd, steps=which.max(cd$modularity)-1)

都给

[1] 3 3 3 2 2 1 1 1 2

我将上述解释为表明每个顶点的社区成员身份是否有误?似乎它(正确地)将 (1,2,3) 分配给社区 3,但错误地将 (4,5,9) 和 (6,7,8) 组合在一起。

【问题讨论】:

  • 也许使用membership(cd)?

标签: r igraph


【解决方案1】:

看似不匹配的原因是 igraph 读取和分配顶点 ID 的顺序(参见 this answer 以供参考)。

我们来看看graph-object提供的顶点顺序:

> unlist(graph, use.names = F)               # identical to c(v1, v2)
 [1] 1 1 2 3 4 4 6 7 8 9 2 3 3 4 5 6 5 8 9 7

在此序列中,5 仅出现在 1,2,3,4,6,7,8,9 之后。这是删除重复条目后得到的顶点序列:

> unique(unlist(graph, use.names = F))
[1] 1 2 3 4 6 7 8 9 5

或使用igraph:

> V(g)
+ 9/9 vertices, named, from 3bae324:
[1] 1 2 3 4 6 7 8 9 5

这与您的示例中的社区非常吻合:

> sort(membership(cd))
7 8 9 4 6 5 1 2 3         # vertex name
1 1 1 2 2 2 3 3 3         # community

由于序列匹配,您可以将成员资格分配为像这样的顶点属性(如 @chinsoon12 所建议的那样):

V(g)$community <- membership(cd)

【讨论】:

  • 谢谢。我发现的另一件事是这样做:community &lt;- cd$membership; v &lt;- as_ids(V(g)); vcom &lt;- data.frame(v,community)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 2014-08-22
  • 1970-01-01
相关资源
最近更新 更多