Adisri99 commited on
Commit
fde9d72
·
verified ·
1 Parent(s): 1ce499f

Update app/graph_features.py

Browse files
Files changed (1) hide show
  1. app/graph_features.py +29 -9
app/graph_features.py CHANGED
@@ -1,12 +1,32 @@
1
  import networkx as nx
 
2
  def build_graph(candidate_pairs):
3
- g=nx.Graph()
4
- for a,b in candidate_pairs: g.add_edge(a,b)
 
5
  return g
6
- def pair_graph_features(g,a,b):
7
- degree_sum=float(g.degree(a)+g.degree(b))
8
- common=len(list(nx.common_neighbors(g,a,b))) if a in g and b in g else 0
9
- na=set(g.neighbors(a)) if a in g else set(); nb=set(g.neighbors(b)) if b in g else set()
10
- union=len(na|nb); inter=len(na&nb); jaccard=float(inter/union) if union else 0.0
11
- nodes=set([a,b])|na|nb; sub=g.subgraph(nodes); possible=max(1,len(nodes)*(len(nodes)-1)/2); density=float(sub.number_of_edges()/possible)
12
- return {"graph_degree_sum":degree_sum,"graph_common_neighbors":float(common),"graph_jaccard":jaccard,"graph_local_density":density}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import networkx as nx
2
+
3
  def build_graph(candidate_pairs):
4
+ g = nx.Graph()
5
+ for a, b in candidate_pairs:
6
+ g.add_edge(a, b)
7
  return g
8
+
9
+ def pair_graph_features(g, a, b):
10
+ degree_map = dict(g.degree())
11
+ degree_sum = float(degree_map.get(a, 0) + degree_map.get(b, 0))
12
+
13
+ common = len(list(nx.common_neighbors(g, a, b))) if a in g and b in g else 0
14
+
15
+ na = set(g.neighbors(a)) if a in g else set()
16
+ nb = set(g.neighbors(b)) if b in g else set()
17
+
18
+ union = len(na | nb)
19
+ inter = len(na & nb)
20
+ jaccard = float(inter / union) if union else 0.0
21
+
22
+ nodes = set([a, b]) | na | nb
23
+ sub = g.subgraph(nodes)
24
+ possible = max(1, len(nodes) * (len(nodes) - 1) / 2)
25
+ density = float(sub.number_of_edges() / possible)
26
+
27
+ return {
28
+ "graph_degree_sum": degree_sum,
29
+ "graph_common_neighbors": float(common),
30
+ "graph_jaccard": jaccard,
31
+ "graph_local_density": density,
32
+ }