阅读有向图的Edgelist

发布于 2025-01-23 08:28:46 字数 847 浏览 0 评论 0原文

我试图将不同的聚类方法应用于我的NetworkX图,该图形很大(2631个边缘和2179个节点)。为此,我想构建/学习嵌入,目前我正在通过Node2Vec算法进行操作。

然而,问题在于,我尚未设法在考虑边缘的方向时弄清楚如何阅读我的edgelist。没有任何权重,因为我没有或需要这些。 我尝试了不同的方法,我当前的代码被这个条目。

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

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

此外,我尝试了几个也接近。

奇怪的是,这些代码(例如示例1)块没有贯穿,但没有丢失错误。它永远不会结束。此外,在此期间,我还可以运行其他几个代码。然后这些偶尔会丢弃错误:错误!会话/行号在数据库中不是唯一的。历史记录搬到了新的会话57

您是否知道read_edgelist变量的其他方法或其他方法也可以存储边缘的方向?

任何帮助都非常感谢,当您有任何疑问或我身边有误会时,请告诉我。

多谢

I am trying to apply different clustering methods to my networkx Graph, which is quite big (2631 edges and 2179 nodes). For that to work I would like to build/learn embeddings, which I am currently doing via the Node2Vec algorithm.

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 (e.g. Example 1) chunks do not run through, but without throwing an error. 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.

Do you know any other approach or addition to the read_edgelist variable to store the direction of the edges as well?

Any help is greatly appreciated, please let me know when you have any questions or there are misunderstandings at my side.

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2025-01-30 08:28:46
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)

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)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文