Reputation: 353
I am working with NetworkX and I have built a weighted graph from an adjacency matrix.
Now I would like to remove all the edges which have a weight lower than a specific threshold. How could I identify those links?
Here is how I construct my graph:
G = nx.from_numpy_matrix(weighted, parallel_edges=False)
Now I would like to remove the edges using that condition writing something like the following, but for edges:
[node for node, degree in dict(G.degree()).items() if degree > 2]
But I get an error when I write G.edges(i).data()
so I am not able to write the condition to evaluate.
Upvotes: 1
Views: 1616
Reputation: 11939
You can first get the edges with the weights with nx.get_edge_attributes
and then remove the one over a certain weight with remove_edges_from
.
Example.
>>> weighted = np.random.randint(10, size=(3, 3))
>>> G = nx.from_numpy_matrix(weighted, parallel_edges=False)
>>> edge_weights = nx.get_edge_attributes(G,'weight')
>>> edge_weights
{(0, 0): 2, (0, 2): 8, (0, 1): 4, (1, 1): 1, (1, 2): 4, (2, 2): 2}
>>> G.remove_edges_from((e for e, w in edge_weights.items() if w > 5))
Upvotes: 3