简体   繁体   English

如何在NetworkX中仅绘制前50-100个连接的组件子图; 一次绘制多个子图

[英]How to graph only top 50-100 connected component subgraphs in NetworkX; drawing multiple subgraphs at once

Sorry if this is rough: it's my first post to Stackoverflow! 抱歉,这很粗糙:这是我对Stackoverflow的第一篇文章! I am sorry in advance for not posting code, but nothing I am doing is complex (and maybe that's the problem), so describing should work. 抱歉,我没有发布代码,但是我所做的事情并不复杂(也许就是问题所在),所以描述应该可以。 I also apologize if I am bad at describing issues because I am new to Python; 如果由于我是Python的新手而无法描述问题,我也深表歉意。 I am not really sure how to recreate an example without data already :( 我不确定如何在没有数据的情况下重新创建示例:(

When using NetworkX, I frequently run large, undirected graphs (let's call them G) with thousands of nodes after importing the data in from pandas. 使用NetworkX时,从熊猫导入数据后,我经常运行具有数千个节点的大型无向图(我们称其为G)。 The VAST majority of nodes only have one or two edges, which are just noise to me. VAST大多数节点只有一个或两个边缘,这对我来说只是个噪音。 It's clusters with lots of nodes that interest me, and that's actually the minority. 它的群集中有许多令我感兴趣的节点,而实际上这只是少数。

So I will then run the nx.connected_components command to make a long list of all the subgraph sets contained with G, review the top results, and print the individual subgraphs that interest me one at a time. 因此,然后我将运行nx.connected_components命令,以列出G包含的所有子nx.connected_components的一长串清单,查看最上面的结果,并一次打印一个我感兴趣的子图。

As such, when I get my generator list/dictionary of all of the connected component subgraphs (which is typically very long), I will also generally just look at the first 50-100 results. 这样,当我获得所有连接的组件子图的生成器列表/字典(通常很长)时,我通常也只会查看前50-100个结果。 Because these tend to have what I am looking for. 因为这些往往具有我要找的东西。

I tried nx.connected_component_subgraphs , but, there are so many I don't need that way that it's almost as bad as just visualizing the whole network at once. 我尝试了nx.connected_component_subgraphs ,但是有太多我不需要这种方式,几乎就像一次可视化整个网络一样糟糕。

So in short: how can I take the generator/list of sets that nx.connected_components gives me--which I then shorten to the top 50--and make that into a new graph? 简而言之:我如何才能将nx.connected_components给我的集合的生成器/列表(然后将其缩短到前50个)并将其生成一个新图形?

I tried converting the output of nx.component_components to a list, but it is all sets. 我尝试将nx.component_components的输出转换为列表,但这是所有集合。

No error messages. 没有错误讯息。

One approach could be something like the following: 一种方法可能类似于以下内容:

First find all components but the N largest ones 首先找到除N个最大分量以外的所有分量

small_components = sorted(nx.connected_components(G), key=len)[:-N]

Then, remove from G all vertices belonging to one of these components: 然后,从G中删除属于这些分量之一的所有顶点:

G.remove_nodes_from(itertools.chain.from_iterable(small_components))

Here's an example where we keep only the two largest components of a given graph: 这是一个示例,其中我们仅保留给定图的两个最大部分:

In [31]: G = nx.Graph()
In [32]: G.add_edges_from([(1, 2), (2, 3), (3, 4), (5, 6), (7, 8), (8, 9)])
In [33]: small_components = sorted(nx.connected_components(G), key=len)[:-2]
In [34]: small_components
Out[34]: [{5, 6}]
In [35]: G.remove_nodes_from(itertools.chain.from_iterable(small_components))
In [36]: G.nodes()
Out[36]: NodeView((1, 2, 3, 4, 7, 8, 9))

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

相关问题 继承networkx图并使用nx.connected_component_subgraphs - inheriting networkx Graph and using nx.connected_component_subgraphs 从 NetworkX 图中查找连接组件内的子图 - Find Subgraphs inside a Connected Component from a NetworkX Graph 完全连接来自networkx中较大图形的子图 - completely connected subgraphs from a larger graph in networkx AttributeError:模块“networkx”没有属性“connected_component_subgraphs” - AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs' 如何使用.networkx 查找强连通分量的子图 - How to find subgraphs of strongly connected components using networkx networkx 库更新 nx.connected_component_subgraphs(G) 后,此错误生成 - after networkx library update nx.connected_component_subgraphs(G), this error genarated 用 networkx 连接子图 - join subgraphs with networkx 如何正确匹配networkx中的边缘属性的子图? - How to match subgraphs with edge attributes in networkx correctly? 将图拆分为&lt;= N个节点的断开连接的子图的算法,挑战是获得最大数量的子图(networkx,python) - Algorithm to split a graph into disconnected subgraphs of <=N nodes, challenge is to obtain the maximum number of subgraphs (networkx, python) max(nx.connected_component_subgraphs(),) 的错误没有属性“connected_component_subgraphs” - Error for max(nx.connected_component_subgraphs(),) no attribute 'connected_component_subgraphs'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM