-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
441 lines (355 loc) · 19.9 KB
/
dataset.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import pandas as pd
import os
import numpy as np
import random
from torch_geometric.utils import from_scipy_sparse_matrix
import scipy.sparse as sp
from scipy.spatial import distance_matrix
from torch_geometric.data import Data
import torch
from utils import sens_correlation
import scipy.sparse as sp
def index_to_mask(node_num, index):
mask = torch.zeros(node_num, dtype=torch.bool)
mask[index] = 1
return mask
def sys_normalized_adjacency(adj):
adj = sp.coo_matrix(adj)
adj = adj + sp.eye(adj.shape[0])
row_sum = np.array(adj.sum(1))
row_sum = (row_sum == 0) * 1 + row_sum
d_inv_sqrt = np.power(row_sum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt).tocoo()
def sparse_mx_to_torch_sparse_tensor(sparse_mx):
"""Convert a scipy sparse matrix to a torch sparse tensor."""
sparse_mx = sparse_mx.tocoo().astype(np.float32)
indices = torch.from_numpy(
np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64))
values = torch.from_numpy(sparse_mx.data)
shape = torch.Size(sparse_mx.shape)
return torch.sparse.FloatTensor(indices, values, shape)
def feature_norm(features):
min_values = features.min(axis=0)[0]
max_values = features.max(axis=0)[0]
return 2 * (features - min_values).div(max_values - min_values) - 1
def build_relationship(x, thresh=0.25):
df_euclid = pd.DataFrame(
1 / (1 + distance_matrix(x.T.T, x.T.T)), columns=x.T.columns, index=x.T.columns)
df_euclid = df_euclid.to_numpy()
idx_map = []
for ind in range(df_euclid.shape[0]):
max_sim = np.sort(df_euclid[ind, :])[-2]
neig_id = np.where(df_euclid[ind, :] > thresh * max_sim)[0]
import random
random.seed(912)
random.shuffle(neig_id)
for neig in neig_id:
if neig != ind:
idx_map.append([ind, neig])
# print('building edge relationship complete')
idx_map = np.array(idx_map)
return idx_map
def load_credit(dataset, sens_attr="Age", predict_attr="NoDefaultNextMonth", path="dataset/credit/", label_number=1000):
# print('Loading {} dataset from {}'.format(dataset, path))
if len(dataset) > 13:
idx_features_labels = pd.read_csv(
os.path.join(path, "{}.csv".format(dataset[:-25])))
else:
idx_features_labels = pd.read_csv(
os.path.join(path, "{}.csv".format(dataset)))
if 'Unnamed: 0' in idx_features_labels.columns:
idx_features_labels = idx_features_labels.drop(['Unnamed: 0'], axis=1)
header = list(idx_features_labels.columns)
header.remove(predict_attr)
header.remove('Single')
# sensitive feature removal
# header.remove('Age')
# # Normalize MaxBillAmountOverLast6Months
# idx_features_labels['MaxBillAmountOverLast6Months'] = (idx_features_labels['MaxBillAmountOverLast6Months']-idx_features_labels['MaxBillAmountOverLast6Months'].mean())/idx_features_labels['MaxBillAmountOverLast6Months'].std()
#
# # Normalize MaxPaymentAmountOverLast6Months
# idx_features_labels['MaxPaymentAmountOverLast6Months'] = (idx_features_labels['MaxPaymentAmountOverLast6Months'] - idx_features_labels['MaxPaymentAmountOverLast6Months'].mean())/idx_features_labels['MaxPaymentAmountOverLast6Months'].std()
#
# # Normalize MostRecentBillAmount
# idx_features_labels['MostRecentBillAmount'] = (idx_features_labels['MostRecentBillAmount']-idx_features_labels['MostRecentBillAmount'].mean())/idx_features_labels['MostRecentBillAmount'].std()
#
# # Normalize MostRecentPaymentAmount
# idx_features_labels['MostRecentPaymentAmount'] = (idx_features_labels['MostRecentPaymentAmount']-idx_features_labels['MostRecentPaymentAmount'].mean())/idx_features_labels['MostRecentPaymentAmount'].std()
#
# # Normalize TotalMonthsOverdue
# idx_features_labels['TotalMonthsOverdue'] = (idx_features_labels['TotalMonthsOverdue']-idx_features_labels['TotalMonthsOverdue'].mean())/idx_features_labels['TotalMonthsOverdue'].std()
# build relationship
if os.path.exists(f'{path}/{dataset}_edges.txt'):
edges_unordered = np.genfromtxt(
f'{path}/{dataset}_edges.txt').astype('int')
else:
edges_unordered = build_relationship(
idx_features_labels[header], thresh=0.7)
np.savetxt(f'{path}/{dataset}_edges.txt', edges_unordered)
features = sp.csr_matrix(idx_features_labels[header], dtype=np.float32)
# print(features)
labels = idx_features_labels[predict_attr].values
idx = np.arange(features.shape[0])
idx_map = {j: i for i, j in enumerate(idx)}
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=int).reshape(edges_unordered.shape)
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
# build symmetric adjacency matrix
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
adj = adj + sp.eye(adj.shape[0])
adj_norm = sys_normalized_adjacency(adj)
adj_norm_sp = sparse_mx_to_torch_sparse_tensor(adj_norm)
edge_index, _ = from_scipy_sparse_matrix(adj)
features = torch.FloatTensor(np.array(features.todense()))
labels = torch.LongTensor(labels)
#
#
import random
random.seed(20)
label_idx_0 = np.where(labels == 0)[0]
label_idx_1 = np.where(labels == 1)[0]
random.shuffle(label_idx_0)
random.shuffle(label_idx_1)
idx_train = np.append(label_idx_0[:min(int(0.5 * len(label_idx_0)), label_number // 2)],
label_idx_1[:min(int(0.5 * len(label_idx_1)), label_number // 2)])
idx_val = np.append(label_idx_0[int(0.5 * len(label_idx_0)):int(0.75 * len(
label_idx_0))], label_idx_1[int(0.5 * len(label_idx_1)):int(0.75 * len(label_idx_1))])
idx_test = np.append(label_idx_0[int(
0.75 * len(label_idx_0)):], label_idx_1[int(0.75 * len(label_idx_1)):])
sens = idx_features_labels[sens_attr].values.astype(int)
sens = torch.LongTensor(sens)
train_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_train))
val_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_val))
test_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_test))
return adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens
def load_bail(dataset, sens_attr="WHITE", predict_attr="RECID", path="dataset/bail/", label_number=1000):
# print('Loading {} dataset from {}'.format(dataset, path))
if len(dataset) > 9:
idx_features_labels = pd.read_csv(
os.path.join(path, "{}.csv".format(dataset[:-30])))
else:
idx_features_labels = pd.read_csv(
os.path.join(path, "{}.csv".format(dataset)))
if 'Unnamed: 0' in idx_features_labels.columns:
idx_features_labels.drop(['Unnamed: 0'], axis=1)
header = list(idx_features_labels.columns)
header.remove(predict_attr)
# build relationship
if os.path.exists(f'{path}/{dataset}_edges.txt'):
edges_unordered = np.genfromtxt(
f'{path}/{dataset}_edges.txt').astype('int')
else:
edges_unordered = build_relationship(
idx_features_labels[header], thresh=0.6)
np.savetxt(f'{path}/{dataset}_edges.txt', edges_unordered)
features = sp.csr_matrix(idx_features_labels[header], dtype=np.float32)
labels = idx_features_labels[predict_attr].values
idx = np.arange(features.shape[0])
idx_map = {j: i for i, j in enumerate(idx)}
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=int).reshape(edges_unordered.shape)
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
# build symmetric adjacency matrix
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
adj = adj + sp.eye(adj.shape[0])
adj_norm = sys_normalized_adjacency(adj)
adj_norm_sp = sparse_mx_to_torch_sparse_tensor(adj_norm)
edge_index, _ = from_scipy_sparse_matrix(adj)
features = torch.FloatTensor(np.array(features.todense()))
labels = torch.LongTensor(labels)
# print(features)
# features = normalize(features)
# adj = adj + sp.eye(adj.shape[0])
# features = torch.FloatTensor(np.array(features.todense()))
# labels = torch.LongTensor(labels)
import random
random.seed(20)
label_idx_0 = np.where(labels == 0)[0]
label_idx_1 = np.where(labels == 1)[0]
random.shuffle(label_idx_0)
random.shuffle(label_idx_1)
idx_train = np.append(label_idx_0[:min(int(0.5 * len(label_idx_0)), label_number // 2)],
label_idx_1[:min(int(0.5 * len(label_idx_1)), label_number // 2)])
idx_val = np.append(label_idx_0[int(0.5 * len(label_idx_0)):int(0.75 * len(
label_idx_0))], label_idx_1[int(0.5 * len(label_idx_1)):int(0.75 * len(label_idx_1))])
idx_test = np.append(label_idx_0[int(
0.75 * len(label_idx_0)):], label_idx_1[int(0.75 * len(label_idx_1)):])
sens = idx_features_labels[sens_attr].values.astype(int)
sens = torch.LongTensor(sens)
train_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_train))
val_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_val))
test_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_test))
return adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens
def load_german(dataset, sens_attr="Gender", predict_attr="GoodCustomer", path="dataset/german", label_number=1000):
# print('Loading {} dataset from {}'.format(dataset, path))
idx_features_labels = pd.read_csv(
os.path.join(path, "{}.csv".format(dataset)))
header = list(idx_features_labels.columns)
header.remove(predict_attr)
header.remove('OtherLoansAtStore')
header.remove('PurposeOfLoan')
# Sensitive Attribute
idx_features_labels['Gender'][idx_features_labels['Gender']
== 'Female'] = 1
idx_features_labels['Gender'][idx_features_labels['Gender'] == 'Male'] = 0
# for i in range(idx_features_labels['PurposeOfLoan'].unique().shape[0]):
# val = idx_features_labels['PurposeOfLoan'].unique()[i]
# idx_features_labels['PurposeOfLoan'][idx_features_labels['PurposeOfLoan'] == val] = i
# # Normalize LoanAmount
# idx_features_labels['LoanAmount'] = 2*(idx_features_labels['LoanAmount']-idx_features_labels['LoanAmount'].min()).div(idx_features_labels['LoanAmount'].max() - idx_features_labels['LoanAmount'].min()) - 1
#
# # Normalize Age
# idx_features_labels['Age'] = 2*(idx_features_labels['Age']-idx_features_labels['Age'].min()).div(idx_features_labels['Age'].max() - idx_features_labels['Age'].min()) - 1
#
# # Normalize LoanDuration
# idx_features_labels['LoanDuration'] = 2*(idx_features_labels['LoanDuration']-idx_features_labels['LoanDuration'].min()).div(idx_features_labels['LoanDuration'].max() - idx_features_labels['LoanDuration'].min()) - 1
#
# build relationship
if os.path.exists(f'{path}/{dataset}_edges.txt'):
edges_unordered = np.genfromtxt(
f'{path}/{dataset}_edges.txt').astype('int')
else:
edges_unordered = build_relationship(
idx_features_labels[header], thresh=0.8)
np.savetxt(f'{path}/{dataset}_edges.txt', edges_unordered)
features = sp.csr_matrix(idx_features_labels[header], dtype=np.float32)
labels = idx_features_labels[predict_attr].values
labels[labels == -1] = 0
idx = np.arange(features.shape[0])
idx_map = {j: i for i, j in enumerate(idx)}
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=int).reshape(edges_unordered.shape)
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
# build symmetric adjacency matrix
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
adj = adj + sp.eye(adj.shape[0])
adj_norm = sys_normalized_adjacency(adj)
adj_norm_sp = sparse_mx_to_torch_sparse_tensor(adj_norm)
edge_index, _ = from_scipy_sparse_matrix(adj)
features = torch.FloatTensor(np.array(features.todense()))
labels = torch.LongTensor(labels)
# features = torch.FloatTensor(np.array(features.todense()))
# labels = torch.LongTensor(labels)
import random
random.seed(20)
label_idx_0 = np.where(labels == 0)[0]
label_idx_1 = np.where(labels == 1)[0]
random.shuffle(label_idx_0)
random.shuffle(label_idx_1)
idx_train = np.append(label_idx_0[:min(int(0.5 * len(label_idx_0)), label_number // 2)],
label_idx_1[:min(int(0.5 * len(label_idx_1)), label_number // 2)])
idx_val = np.append(label_idx_0[int(0.5 * len(label_idx_0)):int(0.75 * len(
label_idx_0))], label_idx_1[int(0.5 * len(label_idx_1)):int(0.75 * len(label_idx_1))])
idx_test = np.append(label_idx_0[int(
0.75 * len(label_idx_0)):], label_idx_1[int(0.75 * len(label_idx_1)):])
sens = idx_features_labels[sens_attr].values.astype(int)
sens = torch.LongTensor(sens)
train_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_train))
val_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_val))
test_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_test))
return adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens
def load_pokec(dataset, sens_attr, predict_attr, path="dataset/pokec/", label_number=1000, sens_number=500, seed=19,
test_idx=False):
"""Load data"""
print('Loading {} dataset from {}'.format(dataset, path))
idx_features_labels = pd.read_csv(os.path.join(path, "{}.csv".format(dataset)))
if 'Unnamed: 0' in idx_features_labels.columns:
idx_features_labels.drop(['Unnamed:0'], axis=1)
header = list(pd.read_csv(os.path.join(path, "{}.csv".format("region_job_z"))).columns)
header2 = list(pd.read_csv(os.path.join(path, "{}.csv".format("region_job_n"))).columns)
header = [i for i in header if i in header2]
header.remove("user_id")
# header.remove(sens_attr)
header.remove(predict_attr)
features = sp.csr_matrix(idx_features_labels[header], dtype=np.float32)
labels = idx_features_labels[predict_attr].values
# build graph
idx = np.array(idx_features_labels["user_id"], dtype=int)
idx_map = {j: i for i, j in enumerate(idx)}
edges_unordered = np.genfromtxt(os.path.join(path, "{}_relationship.txt".format(dataset, )), dtype=int)
edges = np.array(list(map(idx_map.get, edges_unordered.flatten())),
dtype=int).reshape(edges_unordered.shape)
# edges = edges_unordered
adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])),
shape=(labels.shape[0], labels.shape[0]),
dtype=np.float32)
# build symmetric adjacency matrix
adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
# features = normalize(features)
adj = adj + sp.eye(adj.shape[0])
edge_index, _ = from_scipy_sparse_matrix(adj)
features = torch.FloatTensor(np.array(features.todense()))
labels = torch.LongTensor(labels)
# adj = sparse_mx_to_torch_sparse_tensor(adj)
labels[labels > 1] = 1
# labels[labels < 1] = 0
import random
random.seed(seed)
label_idx = np.where(labels >= 0)[0] # 找到label有效的集合
random.shuffle(label_idx)
label_idx_0 = np.where(labels == 0)[0]
label_idx_1 = np.where(labels == 1)[0]
random.shuffle(label_idx_0)
random.shuffle(label_idx_1)
idx_train = np.append(label_idx_0[:min(int(0.5 * len(label_idx_0)), label_number // 2)],
label_idx_1[:min(int(0.5 * len(label_idx_1)), label_number // 2)])
idx_val = np.append(label_idx_0[int(0.5 * len(label_idx_0)):int(0.75 * len(label_idx_0))],
label_idx_1[int(0.5 * len(label_idx_1)):int(0.75 * len(label_idx_1))])
idx_test = np.append(label_idx_0[int(0.75 * len(label_idx_0)):], label_idx_1[int(0.75 * len(label_idx_1)):])
sens = idx_features_labels[sens_attr].values.astype(int)
sens = torch.FloatTensor(sens)
train_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_train))
val_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_val))
test_mask = index_to_mask(features.shape[0], torch.LongTensor(idx_test))
# random.shuffle(sens_idx)
return adj, edge_index, features, labels, train_mask, val_mask, test_mask, sens
def get_dataset(dataname, inid, top_k):
if(dataname == 'credit'):
load, label_num = load_credit, 6000
adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens = load(
dataset=dataname+inid, label_number=label_num)
elif(dataname == 'bail'):
load, label_num = load_bail, 100
adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens = load(
dataset=dataname+inid, label_number=label_num)
elif(dataname == 'pokec'):
adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens = load_pokec(dataset='region_job'+inid,
sens_attr="region",
predict_attr="I_am_working_in_field",
path="dataset/pokec/",
label_number=500,
sens_number=200,
seed=20,
test_idx=False)
# elif (dataname == 'pokec'):
# adj_norm_sp, edge_index, features, labels, train_mask, val_mask, test_mask, sens, idx_sens_train = load_pokec(dataset='region_job_2',
# sens_attr="region",
# predict_attr="I_am_working_in_field",
# path="../dataset/pokec/",
# label_number=500,
# sens_number=200,
# seed=20,
# test_idx=False)
if(dataname == 'credit' ):
sens_idx = 1
elif(dataname == 'bail'):
sens_idx = 0
elif (dataname == 'pokec'):
sens_idx = 3
x_max, x_min = torch.max(features, dim=0)[0], torch.min(features, dim=0)[0]
norm_features = feature_norm(features)
norm_features[:, sens_idx] = features[:, sens_idx]
features = norm_features
corr_matrix = sens_correlation(features, sens_idx)
corr_idx = np.argsort(-np.abs(corr_matrix))
if(top_k > 0):
corr_idx = corr_idx[:top_k]
return Data(x=features, edge_index=edge_index, adj_norm_sp=adj_norm_sp, y=labels.float(), train_mask=train_mask, val_mask=val_mask, test_mask=test_mask, sens=sens), sens_idx, corr_matrix, corr_idx, x_min, x_max