写入文件python并用序号_在python中使用iGraph进行社区检测并将每个节点的社区编号写入CSV...

1586010002-jmsa.png

I have an network that I would like to analyze using the edge_betweenness community detection algorithm in iGraph. I'm familiar with NetworkX, but am trying to learning iGraph because of it's additional community detection methods over NetworkX.

My ultimate goal is to run edge_betweenness community detection and find the optimal number of communities and write a CSV with community membership for each node in the graph.

Below is my code as it currently stands. Any help figuring out community membership is greatly appreciated.

input data ('network.txt'):

1 2

2 3

2 7

3 1

4 2

4 6

5 4

5 6

7 4

7 8

8 9

9 7

10 7

10 8

10 9

iGraph code

import igraph

# load data into a graph

g = igraph.Graph.Read_Ncol('network.txt')

# plot graph

igraph.plot(g)

YWlgy.png

# identify communities

communities = igraph.community_edge_betweenness()

# not really sure what to do next

num_communities = communities.optimal_count

communities.as_clustering(num_communities)

What do I need to do to find the optimal number of communities and write which community each node in the graph belongs to a list?

解决方案

You are on the right track; the optimal number of communities (where "optimal" is defined as "the number of communities that maximizes the modularity score) can be retrieved by communities.optimal_count and the community structure can be converted into a flat disjoint clustering using communities.as_clustering(num_communities). Actually, the number of communities can be omitted if it happens to be equal to communities.optimal_count. Once you've done that, you get a VertexClustering object with a membership property which gives you the cluster index for each vertex in the graph.

For sake of clarity, I'm renaming your communities variable to dendrogram because the edge betweenness community detection algorithm actually produces a dendrogram::

# calculate dendrogram

dendrogram = graph.community_edge_betweenness()

# convert it into a flat clustering

clusters = dendrogram.as_clustering()

# get the membership vector

membership = clusters.membership

Now we can start writing the membership vector along with the node names into a CSV file::

import csv

from itertools import izip

writer = csv.writer(open("output.csv", "wb"))

for name, membership in izip(graph.vs["name"], membership):

writer.writerow([name, membership])

If you are using Python 3, use zip instead of izip and there is no need to import itertools.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值