forked from B-Deprez/NetworkFraud_BiRank_M2V_SAGE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphSAGE_impl.py
More file actions
161 lines (128 loc) · 5.08 KB
/
GraphSAGE_impl.py
File metadata and controls
161 lines (128 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import stellargraph as sg
import pandas as pd
import pickle as pkl
import numpy as np
from stellargraph import StellarGraph
import networkx as nx
from stellargraph.layer import HinSAGE
from stellargraph.mapper import HinSAGENodeGenerator, NodeSequence
from keras import layers
from tensorflow.keras import layers, optimizers, Model
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.callbacks import EarlyStopping
import sklearn
def HinSAGE_embedding(HG, claim_data_features, labels, dimensions= [64,64], batch_size = 50, epochs = 50, train_size=0, val_size=0):
# We will first extract the different nodes and edges
# in order to assign them the necesseary featrues
claim_nodes = pd.DataFrame(index= HG.nodes("claim"))
claim_nodes.index.name = "ID"
contract_nodes = pd.DataFrame(index= HG.nodes("contract"))
contract_nodes.index.name = "ID"
counterparty_nodes = pd.DataFrame(index= HG.nodes("counterparty"))
counterparty_nodes.index.name = "ID"
broker_nodes = pd.DataFrame(index= HG.nodes("broker"))
broker_nodes.index.name = "ID"
nodes = {
"claim": claim_nodes,
"broker": broker_nodes,
"contract": contract_nodes,
"counterparty": counterparty_nodes
}
edges = HG.edges()
# Initialise the features of the different node types
# Only for the claims do we have additional information
# The other features are set to 1, since HinSAGE requires all nodes to have features to work
broker_nodes["Feature"] = 1
contract_nodes["Feature"] = 1
counterparty_nodes["Feature"] = 1
claim_features = claim_data_features[claim_data_features["SI01_NO_SIN"].isin(claim_nodes.index)].reset_index(drop = True).set_index("SI01_NO_SIN")
node_features = {
"claim": claim_features,
"broker": broker_nodes[["Feature"]],
"contract": contract_nodes[["Feature"]],
"counterparty": counterparty_nodes[["Feature"]]
}
# The network is constructed in networkx in order to easily incorporate the features as well
G_nx = nx.Graph()
# For the nodes, iteration over the dictionary is needed
for key, values in nodes.items():
G_nx.add_nodes_from(list(values.index), ntype=key)
# Edges can just be added
G_nx.add_edges_from(edges)
# Construct the stellargraph object
G_sg = sg.StellarGraph.from_networkx(G_nx, node_type_attr="ntype", node_features=node_features)
# We want the index to be sorted in order to have the time dimension right
labels.sort_index(inplace = True)
train_subjects = labels.iloc[:train_size]
val_subjects = labels.iloc[train_size:(train_size+val_size)]
test_subjects = labels.iloc[(train_size+val_size):]
# Set-up of the HinSAGE model
num_samples = [2,32]
embedding_node_type = "claim"
es_callback = EarlyStopping(
monitor="val_auc",
patience=5,
restore_best_weights=True
)
generator = HinSAGENodeGenerator(
G_sg,
batch_size,
num_samples,
head_node_type = embedding_node_type
)
train_gen = generator.flow(
train_subjects.index,
train_subjects["Proven_fraud"]
)
val_gen = generator.flow(
val_subjects.index,
val_subjects["Proven_fraud"]
)
model = HinSAGE(
layer_sizes = dimensions,
generator = generator,
dropout=0)
x_inp, x_out = model.build()
prediction = layers.Dense(
units=1,
activation="sigmoid",
dtype='float32')(x_out)
model = Model(
inputs = x_inp,
outputs=prediction
)
model.compile(
optimizer = optimizers.Adam(lr=1e-3),
loss=binary_crossentropy,
metrics=["AUC"],
)
weights = sklearn.utils.class_weight.compute_class_weight(
'balanced',
classes = np.unique(
labels["Proven_fraud"]
),
y = labels["Proven_fraud"].values
)
weights_dic = {0: weights[0], 1: weights[1]}
model.fit(
train_gen,
epochs = epochs,
validation_data = val_gen,
shuffle=False, # this should be False, since shuffling data means shuffling the whole graph
verbose = 2,
callbacks=[es_callback],
class_weight=weights_dic,
)
test_gen = generator.flow(test_subjects.index, test_subjects["Proven_fraud"])
test_metrics = model.evaluate(test_gen)
print("\nTest Set Metrics:")
for name, val in zip(model.metrics_names, test_metrics):
print("\t{}: {:0.4f}".format(name, val))
# Make both the predictions and the embeddings according to HinSAGE
full_gen = generator.flow(labels.index, labels["Proven_fraud"])
full_prediction = model.predict(full_gen).squeeze()
trained_model = Model(inputs=x_inp, outputs=x_out)
embeddings = trained_model.predict(full_gen)
full_emb = pd.DataFrame(embeddings, index = labels.index)
full_emb["Prediction"] = full_prediction
return full_emb