【问题标题】:how to create overlapping communities in igraph?如何在 igraph 中创建重叠社区?
【发布时间】:2015-01-06 22:42:21
【问题描述】:

我想知道是否可以在 igraph 中创建重叠社区?

df1 :顶点列表及其成员

EDIT:id是指顶点名称,membership是指顶点社区id,所以顶点1是 属于两个社区:32

df1<- structure(list(id = c(1L,2L,3L, 4L, 5L,6L, 7L, 8L,9L), membership = list(c(3, 2),c(2),c(1),c(4),
                                                                             c(4, 1),c(2, 4),
                                                                             c(1),c(1, 3),c(2,1,3))),
                                                           .Names = c("id","membership"),class = "data.frame", row.names = c(NA, -9L))
#id membership
#1  1       3, 2
#2  2          2
#3  3          1
#4  4          4
#5  5       4, 1
#6  6       2, 4
#7  7          1
#8  8       1, 3
#9  9    2, 1, 3

df2 : 具有边缘成员资格的边缘列表

df2<- structure(list(id1 = c(1L,2L,5L,8L,9L,4L,6L),id2 = c(3L,5L,8L,1L,2L,3L,7L), membership = list(c(1),c(2),c(1,4),c(3,2),
                                                                           c(1,3, 4),c(3),c(1,2))),
            .Names = c("id1","id2","membership"),class = "data.frame", row.names = c(NA, -7L))
    #id1 id2 membership
#1   1   3          1
#2   2   5          2
#3   5   8       1, 4
#4   8   1       3, 2
#5   9   2    1, 3, 4
#6   4   3          3
#7   6   7       1, 2

请考虑我们要创建一个社区对象:

  # diffrent membership vector from df1 
mem <-sapply(1:max(sapply(df1$membership, function(x)length(x))),function(y){
  unlist(lapply(df1$membership,function(x) x[y]))})

         # [,1] [,2] [,3]
# [1,]    3    2   NA
# [2,]    2   NA   NA
# [3,]    1   NA   NA
# [4,]    4   NA   NA
# [5,]    4    1   NA
# [6,]    2    4   NA
# [7,]    1   NA   NA
# [8,]    1    3   NA
# [9,]    2    1    3

#create community objects for each membership vector
com<-sapply(1:ncol(mem),function(x) create.communities(membership=mem[,x], algorithm = NULL, merges = NULL,
                        modularity = NULL))

# com
#[[1]]
#Graph community structure calculated with the unknown algorithm
#Number of communities: 4 
#Membership vector:
#[1]  3  2  1  4  4  2  1  1  2

# [[2]]
#Graph community structure calculated with the unknown algorithm
#Number of communities: NA 
#Membership vector:
#[1]  2 NA NA NA  1  4 NA  3  1

#[[3]]
#Graph community structure calculated with the unknown algorithm
#Number of communities: NA 
#Membership vector:
#[1] NA NA NA NA NA NA NA NA  3

   **Edit**:graph=graph.data.frame(df2,directed=FALSE)
    sapply (1:length(com),function(x) plot.communities(com[[x]],graph))

最后一行给我一个错误Error in membership(x) : Cannot calculate community membership,因为它无法计算社区成员中包含 NA 的社区数量

那么,在 igraph 中创建这些重叠社区的解决方案是什么? 提前谢谢你,很抱歉我的长问题。

【问题讨论】:

  • igraph AFAIK 不支持此功能。 create.communities 不适用于重叠社区。​​span>
  • 感谢@GaborCsardi 的评论,有没有其他方法可以让社区重叠?
  • 我不确定您在这里要做什么,但您可以为您的社区定义自己的类。例如。只需将不同的组放在列表中即可。
  • @GaborCsardi,除非您指的是不属于任何社区的顶点,否则我没有收到您的评论。如果您为我澄清这一点,我将不胜感激。我想做的是有重叠的社区,我有顶点列表以及它们所属的社区类 ID。
  • 是的,这就是我要说的。您已经在df1 中拥有您的社区。​​span>

标签: r igraph


【解决方案1】:
  1. 我不清楚df1 中的列名指的是什么。
  2. 您似乎根本没有使用df2。为什么要定义它?
  3. 当它到达函数plot.communities 时出现错误。这是个人功能吗?
  4. 我在代码中的任何地方都没有看到变量 graph,所以这也会产生错误。

无论如何,当你创建mem时,它首先使用df1$membership的第一个值,然后是第二个,等等。对于某些(第2-4、7列),只有一个元素在membership,所以它返回 NA。您可能必须明确检查元素的长度。

因此,您可能想要与此类似的内容(但如果需要,请替换 ifelse 条件):

mem

sapply(df1$membership, function(x)

ifelse(长度(x)

> 内存

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

[1,] 3 2 3

[2,] 2 2 2

[3,] 1 1 1

[4,] 4 4 4

[5,] 4 1 4

[6,] 2 4 2

[7,] 1 1 1

[8,] 1 3 1

[9,] 2 1 3

我还稍微更改了代码(例如去掉了unlist(lapply)。

编辑: 此外,在创建com 时,您可能需要lapply 而不是sapply

【讨论】:

  • 感谢您的回答,请参阅编辑部分定义graphdf2:是边缘列表)和df1 colnames。 plot.communities 不是个人功能:请看这个link
  • 会以某种方式考虑解决方案,但假设从一开始就有一些不属于任何社区的顶点。
猜你喜欢
  • 2016-04-30
  • 2017-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多