简体   繁体   中英

Remove edges having a weight in a specific range in networkx

I have a dataframe. I converted that dataframe to a graph. After that, I wanted to remove edges in a specific weight range:

Columns:

df.columns:
Index(['source', 'target', 'weight'], dtype='object')

Length:

len(df)
1048575

Data types:

df.dtypes
source      int64
target    float64
weight      int64
dtype: object

Now, building a networkx graph:

Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)

Graph information:

print(nx.info(G))
Name: 
Type: Graph
Number of nodes: 609627
Number of edges: 915549
Average degree:   3.0036

Degrees :

degrees = sorted(G.degree, key=lambda x: x[1], reverse=True)
degrees
[(a, 1111),
 (c, 1107),
 (f, 836),
 (g, 722),
 (h, 608),
 (k, 600),
 (r, 582),
 (z, 557),
 (l, 417), etc....

What i want to do is to remove edges with a specific weight. For example, i want to remove all edges with weight > = 500

to_remove = [(a,b) for a,b in G.edges(data=True) if "weight" >= 500]
G.remove_edges_from(to_remove)

However, i get the following error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-df3a67f18df9> in <module>()
----> 1 to_remove = [(a,b) for a,b in G.edges(data=True) if "weight" >=500]

<ipython-input-12-df3a67f18df9> in <listcomp>(.0)
----> 1 to_remove = [(a,b) for a,b in G.edges(data=True) if "weight" >=500]

ValueError: too many values to unpack (expected 2)

Any ideas why am i getting this message? or perhaps there is a better way to do that? Thank you

This is how I attacked this problem:

threshold = 500

# filter out all edges above threshold and grab id's
long_edges = list(filter(lambda e: e[2] > threshold, (e for e in G.edges.data('weight'))))
le_ids = list(e[:2] for e in long_edges)

# remove filtered edges from graph G
G.remove_edges_from(le_ids)

In this comprehension, you're comparing a string "weight" to an int, which doesn't make much sense:

[(a,b) for a,b in G.edges(data=True) if "weight" >= 500]

Also, what really causes the exception, if you pass data=True , you'll get a 3-tuple, where the third element is a dict of properties.

What you probably wanted to do is:

[(a,b) for a, b, attrs in G.edges(data=True) if attrs["weight"] >= 500]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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