library(igraph)
library(NMI)
library(ggplot2)
data <- read.table("D:/war3/矩阵数据/其他数据/fb-pages-food/fb-pages-food.txt")
graph <- graph.data.frame(data, directed = FALSE)
true_num_communities <- 18
ground_truth_labels <- cluster_louvain(graph)$membership
mixing_params <- seq(0.10, 0.50, by = 0.10)
results <- data.frame(Mixing_Param = numeric(), Degree_Modularity_NMI = numeric(), Improved_Random_Blockmodel_NMI = numeric(),
Degree_Modularity_Time = numeric(), Improved_Random_Blockmodel_Time = numeric())
for (param in mixing_params) {
start_time <- Sys.time()
degree_modularity_com <- cluster_fast_greedy(graph)
degree_modularity_time <- difftime(Sys.time(), start_time, units = "secs")
start_time <- Sys.time()
improved_blockmodel_com <- cluster_edge_betweenness(graph)
improved_blockmodel_time <- difftime(Sys.time(), start_time, units = "secs")
degree_modularity_labels <- membership(degree_modularity_com)
improved_blockmodel_labels <- membership(improved_blockmodel_com)
degree_modularity_nmi <- NMI::NMI(degree_modularity_labels, ground_truth_labels)
improved_blockmodel_nmi <- NMI::NMI(improved_blockmodel_labels, ground_truth_labels)
results <- rbind(results, c(param, degree_modularity_nmi, improved_blockmodel_nmi, degree_modularity_time, improved_blockmodel_time))
}
怎么修改啊,哪里出问题了
导入数据
data <- read.table("D:/war3/矩阵数据/其他数据/fb-pages-food/fb-pages-food.txt")
创建图对象
graph <- graph.data.frame(data, directed = FALSE)
设置真实社区数目
true_num_communities <- 18
自动分配真实社区标签
ground_truth_labels <- cluster_louvain(graph)$membership
设置混合参数范围
mixing_params <- seq(0.10, 0.50, by = 0.10)
存储结果
results <- data.frame(Mixing_Param = numeric(), Degree_Modularity_NMI = numeric(), Improved_Random_Blockmodel_NMI = numeric(),
Degree_Modularity_Time = numeric(), Improved_Random_Blockmodel_Time = numeric())
遍历不同的混合参数
for (param in mixing_params) {
使用度数修正模型进行社区检测
start_time <- Sys.time()
degree_modularity_com <- cluster_fast_greedy(graph, modularity = NULL, links = NULL, parameter = param)
degree_modularity_time <- difftime(Sys.time(), start_time, units = "secs")
使用改进的随机块模型进行社区检测
start_time <- Sys.time()
improved_blockmodel <- cluster_edge_betweenness(graph, modularity = NULL, links = NULL, parameter = param)
improved_blockmodel_time <- difftime(Sys.time(), start_time, units = "secs")
获取自动划分的社区标签
degree_modularity_labels <- cluster_fast_greedy(degree_modularity_com)$membership