python - 如何使用 R (igraph) 或 python 对不同类型的不同节点集进行建模?

标签 python r csv igraph vertices

我需要一些有关多列数据建模的帮助。我有 .csv 文件。我有一份边缘名单,列出了人员的年龄、性别、地点和所患疾病。我画了一个患有疾病的人的二分图。如何使用 igraph 读取二分图中的年龄、性别和位置?我尝试了下面的代码,但只需要 2 列 csv 即可绘制网络。在这种情况下,任何人都可以帮助如何读取年龄、性别和位置等人员属性吗?

 Person   Diseases   Age    gender  location    
    Person1 Asthma  25  Female  Location1   
    Person2 Pneumonia   35  Male    Location2   
    Person3 Typhoid 40  Male    Location3   

  getwd()
    datafile <- "/d.csv"
    d_el <- read.csv(datafile)
    d_el <- d_el[, 1:4 ]
    head(d_el)
library(igraph)
 g <- graph.data.frame(d_el, directed = FALSE)
plot(g, layout = pref.layout, 
     vertex.color="black"
    )

我已经使用了这行代码。还有输出代表什么?

V(g)$Person<- d_el$Person
 V(g)$location<- d_el$location
 V(g)$location[which(V(g)$Person %in% neighbors(g, "TB"))] 

最佳答案

首先,为了从边列表创建二分网络,igraph 将前两列读取为节点和事件,除非您另有说明。然后,您需要通过向属性“type”添加名称向量来告诉 igraph 网络是二分的。因此,要么用节点和事件指示列,要么对列重新排序,以便“人员”和“疾病”位于前两列中。

library(igraph)
d_el <- d_el[,c(1,4,2,3)] #Reorder columns
g <- graph.data.frame(d_el, directed = FALSE)
V(g)$type <- V(g)$name %in% d_el[,1]
g 
#View the igraph object and you will see it say something like 
#"IGRAPH DN-B" - the B shows it's bipartite

使用类似的语法添加属性:

V(g)$age <- d_el$Age
V(g)$gender <- d_el$Gender

要根据与“疾病”的隶属关系获取节点集,请使用 neighbors功能。通过根据邻居对属性进行子集化来获取属性信息:

neighbors(g, "Asthma") #Gets all the names of nodes affiliated with Asthma
V(g)$gender[which(V(g)$name == as_ids(neighbors(g, "Asthma")))]
#Gets the gender of all those with asthma

要提取可以表示为网络(绘制为网络等)的 igraph 对象,请使用 asthma <- make_ego_graph(g,1,"Asthma") .

关于python - 如何使用 R (igraph) 或 python 对不同类型的不同节点集进行建模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998264/

相关文章:

python - 为什么其他窗口(GUI)在Python中运行时其他窗口(GUI)没有打开?

.NET 与 MATLAB 或 R 相结合?

r - 如何分隔向量中的整数和 double ?

java - 如何使用 OpenCSV 将 csv 导入 FX SimpleObjectProperties?

java - Apache CSV 解析器 : Issue with ignoring empty lines

mysql - AWS RDS上MySQL一般查询日志的大小

python - 连续调用 cProfile/pstats 无法正确更新

python - 数据框列的平均值

r - ggplot2 极坐标轴标签位置

python - 如何将一行 append 到另一个数据框