这可能是一个非常简单的问题.我是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()
Python中的print()调用对象的__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的打印方法甚至可以打印出漂亮的文本树状图:
>>> 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)
在这里,我们根据顶点的群集(社区)成员资格为顶点着色: