-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_classification.py
More file actions
185 lines (148 loc) · 6.36 KB
/
Copy pathnode_classification.py
File metadata and controls
185 lines (148 loc) · 6.36 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import time
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import f1_score
from sklearn.linear_model import LogisticRegression
def node_classification(model, adj_matrix, edge_list, node_labels, task_config):
scaled = task_config['scaled']
clf_type = task_config['clf_type']
n_splits = task_config['n_splits']
random_state = task_config['random_state']
embeddings_savepath = task_config['embeddings_savepath']
use_pretrained_embeddings = task_config['use_pretrained_embeddings']
np.random.seed(random_state)
since = time.time()
print('Compute node embeddings on the train graph split.')
if not use_pretrained_embeddings:
model.initialize(adj_matrix, edge_list)
model.build()
embeddings = model.learn_embeddings(embeddings_savepath)
else:
embeddings = model.load_embeddings(embeddings_savepath)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
if scaled:
scaler = StandardScaler()
embeddings = scaler.fit_transform(embeddings)
skf = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=True)
scores_f1 = []
print('Stratified KFold shuffle split')
for i, (index_train, index_test) in enumerate(skf.split(node_labels, node_labels)):
X_train, X_test = embeddings[index_train], embeddings[index_test]
y_train, y_test = node_labels[index_train], node_labels[index_test]
if clf_type == 'logistic':
clf = LogisticRegression(solver='saga')
else:
clf = LogisticRegression(solver='saga')
print('Train a classifier')
clf.fit(X_train, y_train)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
print('Make predictions')
preds_label = clf.predict(X_test)
score_f1 = f1_score(y_test, preds_label, average='macro')
scores_f1.append(score_f1)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
print(f'F1 score: {score_f1:.6f}')
results = {
'f1': scores_f1
}
return results
def node_classification_multilabel(model, adj_matrix, edge_list, node_labels, task_config):
scaled = task_config['scaled']
clf_type = task_config['clf_type']
n_splits = task_config['n_splits']
random_state = task_config['random_state']
embeddings_savepath = task_config['embeddings_savepath']
use_pretrained_embeddings = task_config['use_pretrained_embeddings']
np.random.seed(random_state)
since = time.time()
print('Compute node embeddings on the train graph split.')
if not use_pretrained_embeddings:
model.initialize(adj_matrix, edge_list)
model.build()
embeddings = model.learn_embeddings(embeddings_savepath)
else:
embeddings = model.load_embeddings(embeddings_savepath)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
if scaled:
scaler = StandardScaler()
embeddings = scaler.fit_transform(embeddings)
kf = KFold(n_splits=n_splits, random_state=random_state, shuffle=True)
scores_f1 = []
print('Stratified KFold shuffle split')
for i, (index_train, index_test) in enumerate(kf.split(node_labels)):
X_train, X_test = embeddings[index_train], embeddings[index_test]
y_train, y_test = node_labels[index_train], node_labels[index_test]
if clf_type == 'logistic':
clf = LogisticRegression(solver='saga')
else:
clf = LogisticRegression(solver='saga')
clf = OneVsRestClassifier(clf, n_jobs=21)
print('Train a classifier')
clf.fit(X_train, y_train)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
print('Make predictions')
preds_label = clf.predict(X_test)
score_f1 = f1_score(y_test, preds_label, average='weighted')
scores_f1.append(score_f1)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
print(f'F1 score: {score_f1:.6f}')
results = {
'f1': scores_f1
}
return results
def node_classification_graphconvolution(model, adj_matrix_train, adj_matrix_valid, edge_list_train, edge_list_valid,
node_labels, task_config):
scaled = task_config['scaled']
clf_type = task_config['clf_type']
n_splits = task_config['n_splits']
random_state = task_config['random_state']
embeddings_savepath = task_config['embeddings_savepath']
use_pretrained_embeddings = task_config['use_pretrained_embeddings']
np.random.seed(random_state)
since = time.time()
print('Compute node embeddings on the train graph split.')
if not use_pretrained_embeddings:
model.initialize(adj_matrix_train, edge_list_train)
model.set_valid_set(adj_matrix_valid, edge_list_valid)
model.build()
embeddings = model.learn_embeddings(embeddings_savepath)
else:
embeddings = model.load_embeddings(embeddings_savepath)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
if scaled:
scaler = StandardScaler()
embeddings = scaler.fit_transform(embeddings)
skf = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=True)
scores_f1 = []
print('Stratified KFold shuffle split')
for i, (index_train, index_test) in enumerate(skf.split(node_labels, node_labels)):
X_train, X_test = embeddings[index_train], embeddings[index_test]
y_train, y_test = node_labels[index_train], node_labels[index_test]
if clf_type == 'logistic':
clf = LogisticRegression(solver='saga')
else:
clf = LogisticRegression(solver='saga')
print('Train a classifier')
clf.fit(X_train, y_train)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
print('Make predictions')
preds_label = clf.predict(X_test)
score_f1 = f1_score(y_test, preds_label, average='macro')
scores_f1.append(score_f1)
print('--- complete ---')
print(f'Elapsed: {time.time() - since:.4f}')
print(f'F1 score: {score_f1:.6f}')
results = {
'f1': scores_f1
}
return results