I have two files': nodes.csv and edges.csv that contains nodes and edges between them as follow nodes.csv (182 nodes) look like
0
1
2
.
.
.
and edges represented by 2 columns (300 edges) as
1 2
0 2
.
.
I want to represent nodes and edges that is in these files by graph and calculate the common neighbors between each pair of nodes that is represented by edges
I have trying with code
import networkx as nx
import pandas as pd
G=nx.Graph()
node= pd.read_csv("nodes.csv")
edges=pd.read_csv("edges.csv")
for row in node:
G.add_nodes_from(row)
for row in edges:
if len(row) == 2 : # add an edge only if both values are provided
G.add_edge(row[0],row[1])
print(G) # this is give my Graph with 4 nodes and 1 edges
def predict(u, v):
cnbors = list(nx.common_neighbors(G, u, v))
mult_val = G.degree(u) * G.degree(v)
if mult_val == 0:
return 0
else:
return len(cnbors)/ mult_val
for row in edges:
predict(row[0], row[1]) # this is give me an error: u is not in the graph
First you can produce your graph object by reading edges.csv file using
read_edgelist. There may possibly be some nodes that are not in the edges.csv file, hence you can add them manually later. The graph is ready with the code below:Now you can simply get all possible pairs of nodes and their common neighbors like this:
The result is a list of tuples each of which with three items, containing the first node, the second node, and the list of common neighbors.