gpt4 book ai didi

python - 我如何显示 igraph 输出?

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:29 24 4
gpt4 key购买 nike

这可能是一个非常简单的问题。我是 python 的新手,非常感谢所有帮助!

在我放在下面的代码中,我如何实际显示输出而不是内存对象?

Graph.clusters(g)
Out[106]: <igraph.clustering.VertexClustering at 0x1187659d0>

Graph.community_edge_betweenness(g, clusters=None, directed=True, weights=None)
Out[107]: <igraph.clustering.VertexDendrogram at 0x118765d90>

最佳答案

这取决于你到底想展示什么?让我们举个例子:

import igraph
g = igraph.Graph.Barabasi(n = 20, m = 3)
c = g.clusters()

print()在 Python 中调用 __str__()对象的方法,在 VertexClustering 的情况下将其转换为人类可读的内容,每一行代表一个簇(方括号中的簇ID),并列出属于该簇的顶点ID。第一行给出了简单的描述:

>>> print(c)
Clustering with 20 elements and 1 clusters
[0] 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

然后,您可以像这样访问每个集群的成员作为顶点 ID 列表:

>>> c[0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

如果是VertexDendrogram对象,igraph 的 print 方法甚至打印出漂亮的文本树状图:

>>> f = g.community_fastgreedy()
>>> print(f)
Dendrogram, 20 elements, 19 merges

7 3 14 10 5 16 1 0 9 8 6 2 4 18 12 13 19 15 17 11
| | | | | | | | | | | | | | | | | | | |
`-' | `--' | | | | `-' | `-' `--' | | `--'
| | | | | | | | | | | | | |
`--' | | `-' | `--' | | | `---'
| | | | | | | | | |
| | `---' | | | | `----'
| | | | | | | |
`-----' `----' | `----' |
| | | | |
| `------' `---------'
| | |
`-------------' |
| |
`----------------------'

最后,您可以使用 igraph 出色的绘图功能显示您的结果:

i = g.community_infomap()
colors = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00"]
g.vs['color'] = [None]
for clid, cluster in enumerate(i):
for member in cluster:
g.vs[member]['color'] = colors[clid]
g.vs['frame_width'] = 0
igraph.plot(g)

在这里,我们根据它们的集群(社区)成员身份为顶点着色:

vertices colored by cluster membership

关于python - 我如何显示 igraph 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com