forked from qiyuangong/Mondrian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymizer.py
176 lines (162 loc) · 5.24 KB
/
anonymizer.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
"""
run mondrian with given parameters
"""
# !/usr/bin/env python
# coding=utf-8
from mondrian import mondrian
from utils.read_adult_data import read_data as read_adult
from utils.read_informs_data import read_data as read_informs
import sys, copy, random
DATA_SELECT = 'a'
RELAX = False
INTUITIVE_ORDER = None
def get_result_one(data, k=10):
"""
run mondrian for one time, with k=10
"""
print "K=%d" % k
data_back = copy.deepcopy(data)
result, eval_result = mondrian(data, k, RELAX)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
data = copy.deepcopy(data_back)
print "NCP %0.2f" % eval_result[0] + "%"
print "Running time %0.2f" % eval_result[1] + " seconds"
def get_result_k(data):
"""
change k, whle fixing QD and size of dataset
"""
data_back = copy.deepcopy(data)
# for k in [2, 5, 10, 25, 50, 100]:
for k in range(5, 105, 5):
print '#' * 30
print "K=%d" % k
result, eval_result = mondrian(data, k, RELAX)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
data = copy.deepcopy(data_back)
print "NCP %0.2f" % eval_result[0] + "%"
print "Running time %0.2f" % eval_result[1] + " seconds"
def get_result_dataset(data, k=10, num_test=10):
"""
fix k and QI, while changing size of dataset
num_test is the test nubmber.
"""
data_back = copy.deepcopy(data)
length = len(data_back)
joint = 5000
dataset_num = length / joint
if length % joint == 0:
dataset_num += 1
for i in range(1, dataset_num + 1):
pos = i * joint
ncp = rtime = 0
if pos > length:
continue
print '#' * 30
print "size of dataset %d" % pos
for j in range(num_test):
temp = random.sample(data, pos)
result, eval_result = mondrian(temp, k, RELAX)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
ncp += eval_result[0]
rtime += eval_result[1]
data = copy.deepcopy(data_back)
ncp /= num_test
rtime /= num_test
print "Average NCP %0.2f" % ncp + "%"
print "Running time %0.2f" % rtime + " seconds"
print '#' * 30
def get_result_qi(data, k=10):
"""
change nubmber of QI, whle fixing k and size of dataset
"""
data_back = copy.deepcopy(data)
num_data = len(data[0])
for i in reversed(range(1, num_data)):
print '#' * 30
print "Number of QI=%d" % i
result, eval_result = mondrian(data, k, RELAX, i)
if DATA_SELECT == 'a':
result = covert_to_raw(result)
data = copy.deepcopy(data_back)
print "NCP %0.2f" % eval_result[0] + "%"
print "Running time %0.2f" % eval_result[1] + " seconds"
def covert_to_raw(result):
"""
during preprocessing, categorical attrbutes are covert to
numeric attrbute using intutive order. This function will covert
this values back to they raw oder
"""
covert_result = []
qi_len = len(INTUITIVE_ORDER)
for record in result:
covert_record = []
for i in range(qi_len):
if len(INTUITIVE_ORDER[i]) > 0:
vtemp = ''
if ',' in record[i]:
temp = record[i].split(',')
raw_list = []
for j in range(int(temp[0]), int(temp[1]) + 1):
raw_list.append(INTUITIVE_ORDER[i][j])
vtemp = ';'.join(raw_list)
else:
vtemp = INTUITIVE_ORDER[i][int(record[i])]
covert_record.append(vtemp)
else:
covert_record.append(record[i])
covert_result.append(covert_record)
return covert_result
if __name__ == '__main__':
FLAG = ''
LEN_ARGV = len(sys.argv)
try:
MODEL = sys.argv[1]
DATA_SELECT = sys.argv[2]
FLAG = sys.argv[3]
except IndexError:
pass
INPUT_K = 10
# read record
if MODEL == 's':
RELAX = False
else:
RELAX = True
if RELAX:
print "Relax Mondrian"
else:
print "Strict Mondrian"
if DATA_SELECT == 'i':
print "INFORMS data"
DATA = read_informs()
else:
print "Adult data"
# INTUITIVE_ORDER is an intutive order for
# categorical attrbute. This order is produced
# by the reading (from dataset) order.
DATA, INTUITIVE_ORDER = read_adult()
if FLAG == 'k':
get_result_k(DATA)
elif FLAG == 'qi':
get_result_qi(DATA)
elif FLAG == 'data':
get_result_dataset(DATA)
elif FLAG == '':
get_result_one(DATA)
else:
try:
INPUT_K = int(FLAG)
get_result_one(DATA, INPUT_K)
except ValueError:
print "Usage: python anonymizer [r|s] [a | i] [k | qi | data]"
print "r: relax mondrian, s: strict mondrian"
print "a: adult dataset, i: INFORMS ataset"
print "k: varying k"
print "qi: varying qi numbers"
print "data: varying size of dataset"
print "example: python anonymizer a 10"
print "example: python anonymizer a k"
# anonymized dataset is stored in result
print "Finish Mondrian!!"