首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python iGraph - community_infomap图

Python iGraph - community_infomap图
EN

Stack Overflow用户
提问于 2016-06-24 22:47:41
回答 1查看 2.3K关注 0票数 0

我用networkx制作了图形,保留了70%的权重最大的分支,然后转换成图形,使用community_infomap。图上的标签非常重要。如果你比较community_infomap的图形和结果,你会看到一些奇怪的东西。在图中,很明显有两个社区-信号1,2和第二组3.4.5.6.7.8,但是!在信息映射它弄乱了标签之后,它将信号3.2和1.4.5.6.7.8分组。为什么,会发生什么呢?

代码语言:javascript
复制
def nlargest_indices_orig(full, n):
full = full.copy()
x = np.zeros(n)
y = np.zeros(n)

for idx in range(n):
    x[idx] = np.unravel_index(full.argmax(), full.shape)[0]
    y[idx] = np.unravel_index(full.argmax(), full.shape)[1]
    full[full == full.max()] = 0.

return x, y

labels=[1,2,3,4,5,6,7,8]
o1 = scipy.io.loadmat('out.mat')
X=(o1['out'])
K=np.zeros((8,8))
m, n = np.shape(X)
G = nx.Graph()
for i in range(8):
    for j in range(8):
        if X[i,j]>0:
            s=labels[i]
            b=labels[j]
            w=X[i,j]
            G.add_edge(s,b,weight=w)
B=G.edges()
ND=len(B)
print('Grana ukupno')
procenat=round(0.7*ND)
x,y=nlargest_indices_orig(X, procenat)
s1=x
s2=y
for i in range(len(s2)):
    K[s1[i],s2[i]]=X[s1[i],s2[i]]
np.fill_diagonal(K, 0)
F = nx.Graph()

for i in range(8):
    for j in range(8):
        if K[i,j]>0:
            s=labels[i]
            b=labels[j]
            w=X[i,j]
            F.add_edge(s,b,weight=w)
edgewidth=[]
edgelabels={}
pos = nx.spring_layout(F) # position the nodes by force layout
plt.figure()
plt.axis('off')
print('Weighted graph')
for (u,v,d) in F.edges(data=True):
    print(u,v,d)
    edgewidth.append(d['weight'])
    edgelabels[(u,v)] = d['weight']
nx.draw_networkx_edges(F,pos,width=edgewidth,edge_color='r')
nx.draw_networkx_nodes(F,pos, alpha=0.8, node_size=400,node_color='w',scale=100)
nx.draw_networkx_labels(F,pos, font_size=12)
pylab.savefig('Graf--odabrani-sum imf.png')
plt.show()
edges = F.edges()
nodes=F.nodes()
cg = ig.Graph(edges)
cg.vs['label'] = labels
singletons = cg.vs.select(_degree = 0)
cg.delete_vertices(singletons)
degree = 0
community = cg.community_infomap()
print(len(community))
b=ig.plot(community,'infomap-odabrani-sum imf-.png')
EN

回答 1

Stack Overflow用户

发布于 2016-11-20 23:16:08

为了找出这里发生了什么,我们需要了解igraph的顶点索引行为。顶点索引不稳定,在删除或添加顶点或边时可能会发生重新索引。索引总是从0n-1。如果你初始化一个带有整数元组的图,那么igraph会将这些整数视为顶点索引。你的索引从1开始,所以igraph将引入一个带有索引0的单例顶点,这对你来说是完全错误的,因为它不在你的原始网络中。

代码语言:javascript
复制
import igraph as ig
import networkx as nx
import pylab as plt

_edges = [
    (1, 2), 
    (3, 4), (3, 5), (3, 6), (3, 8),
    (4, 5), (4, 6), (4, 7), (4, 8),
    (5, 6), (5, 7), (5, 8),
    (6, 7), (6, 8),
    (7, 8)
]

G = nx.Graph()

for e in _edges:
    G.add_edge(e[0], e[1], weight = 1)

pos = nx.spring_layout(G)
plt.figure()
plt.axis('off')
nx.draw_networkx_edges(G, pos, edge_color='r')
nx.draw_networkx_nodes(G, pos, alpha=0.8, node_size=400, node_color='w', scale=100)
nx.draw_networkx_labels(G, pos, font_size=12)
plt.show()
plt.savefig('nx_graph1.png')

edges_from_nx = G.edges()

尝试按照您所做的那样初始化igraph.Graph对象:

代码语言:javascript
复制
g = ig.Graph(edges_from_nx)

g.vcount() # returns 9. why? we supplied integers to igraph,
           # what it considered as vertex indices

'name' in g.vs.attributes() # returns False: vertices do not have names

ig.plot(g, vertex_label = range(g.vcount()))

请注意索引为0的单个顶点,您随后删除了该顶点,因此所有其他顶点恰好以不可预测的方式重新索引。

要避免这种情况,请使用TupleList构造函数初始化Graph对象:

代码语言:javascript
复制
g = ig.Graph.TupleList(edges_from_nx)
g.vcount() # returns 8. this looks fine!
'name' in g.vs.attributes() # returns True: good, labels are assigned
                            # to the `name` vertex attribute
ig.plot(g, vertex_label = g.vs['name'])

现在,一切看起来都和NetworkX中的一样。注意:这与community_infomap无关。如果您希望igraph中的顶点或边具有稳定的标识符,请将它们保留在顶点或边属性中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38016211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档