简体   繁体   English

读取有向图的边列表

[英]Read edgelist of directed Graph

I am trying to apply different clustering methods to my.networkx Graph, which is quite big (2631 edges and 2179 nodes).我正在尝试将不同的聚类方法应用于 my.networkx Graph,它非常大(2631 条边和 2179 个节点)。 For that to work I would like to build/learn embeddings, which I am currently doing via the Node2Vec algorithm.为此,我想构建/学习嵌入,我目前正在通过 Node2Vec 算法进行。

The problem however is that I didn't yet manage to figure out how to read my edgelist while considering the direction of the edges.然而,问题是我还没有设法在考虑边缘方向的同时弄清楚如何阅读我的边缘列表。 Without any weights tho, as I do not have or need those.没有任何重量,因为我没有或不需要那些。 I tried out different approaches, my current code is inpired by this entry.我尝试了不同的方法,我当前的代码是受此条目启发的。

nx.write_edgelist(G, "test.edgelist")

G_edges = nx.read_edgelist("test.edgelist")

Moreover, I tried several of those approaches as well.此外,我也尝试了其中的几种方法。

Oddly enough, these Code (eg Example 1) chunks do not run through, but without throwing an error.奇怪的是,这些代码(例如示例 1)块没有运行,但没有抛出错误。 It just never finishes.它永远不会结束。 Additionally, I am able to run several other chucks of code in the meantime.此外,我还可以同时运行其他几个代码块。 These then occasionally throw errors claiming: ERROR.这些然后偶尔会抛出错误声称:错误。 Session/line number was not unique in database.会话/行号在数据库中不是唯一的。 History logging moved to new session 57 .历史记录移至新的 session 57

Do you know any other approach or addition to the read_edgelist variable to store the direction of the edges as well?你知道read_edgelist变量的任何其他方法或添加来存储边缘的方向吗?

Any help is greatly appreciated, please let me know when you have any questions or there are misunderstandings at my side.非常感谢任何帮助,如果您有任何疑问或对我有误解,请告诉我。

Thanks a lot非常感谢

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx


# make dummy directed graph
adj_matrix = np.array([[0,1,1], [0,1,1], [0,1,1]])

G = nx.from_numpy_matrix(adj_matrix, create_using=nx.DiGraph)
# store positions for later use:
pos = nx.spring_layout(G)

# write edgelist. Directionality is implied by ordering of the nodes in the edge
# i.e. 0 1 means an edge from 0 to 1
# and 1 0 means an edge from 1 to 0
nx.write_edgelist(G, 'test.txt')

# to make use of the directional information you need to create a graph 
# that supports directed edges
H = nx.read_edgelist('test.txt', create_using=nx.DiGraph)
H = nx.relabel_nodes(H, {a:int(a) for a in H.nodes()})


# the default nx.Graph class will lose edge direction:
I = nx.read_edgelist('test.txt')
I = nx.relabel_nodes(I, {a:int(a) for a in I.nodes()})
    

fig, axes = plt.subplots(ncols=3)

for ax, graph, title in zip(axes, [G, H, I], ['original', 'nx.DiGraph', 'nx.Graph']):
    nx.draw_networkx(graph, pos=pos, ax=ax)
    ax.set_title(title)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM