Created
August 3, 2022 22:36
-
-
Save ernestofgonzalez/12639f30b981ac405cc34ab8e8fa93fd to your computer and use it in GitHub Desktop.
Computing Adamic-Adar index for a specific node in a graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import pandas as pd | |
def main(): | |
print("Creating graph...") | |
G = nx.dense_gnm_random_graph(4000, 20000) | |
print("Created graph: ", G) | |
print("nodes", list(G)) | |
nodes = list(G) | |
u = 0 | |
nodes.remove(u) | |
ebunch = [(u, x) for x in nodes] | |
print("Ebunch", ebunch) | |
print("Making link predictions...") | |
preds = nx.adamic_adar_index(G, ebunch=ebunch) | |
print("Made link predictions.") | |
preds_df = pd.DataFrame(preds, columns=["node_1", "node_2", "adamic_adar_index"]) | |
print("20 Best link predictions for node 0: ") | |
sorted_preds_df = preds_df.sort_values(by="adamic_adar_index", ascending=False) | |
print(sorted_preds_df[:20]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment