Update app/graph_features.py
Browse files- 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:
|
|
|
|
| 5 |
return g
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|