Skip to content

Instantly share code, notes, and snippets.

@julian-west
Created September 7, 2019 17:01
Show Gist options
  • Save julian-west/dbc6b99d5fc5ab2cc9a77780450930f8 to your computer and use it in GitHub Desktop.
Save julian-west/dbc6b99d5fc5ab2cc9a77780450930f8 to your computer and use it in GitHub Desktop.
# 'winner takes all' method - set minium correlation threshold to remove some edges from the diagram
threshold = 0.5
# create a new graph from edge list
Gx = nx.from_pandas_edgelist(edges, 'asset_1', 'asset_2', edge_attr=['correlation'])
# list to store edges to remove
remove = []
# loop through edges in Gx and find correlations which are below the threshold
for asset_1, asset_2 in Gx.edges():
corr = Gx[asset_1][asset_2]['correlation']
#add to remove node list if abs(corr) < threshold
if abs(corr) < threshold:
remove.append((asset_1, asset_2))
# remove edges contained in the remove list
Gx.remove_edges_from(remove)
print(str(len(remove)) + " edges removed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment