-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMondrian.py
More file actions
20 lines (18 loc) · 719 Bytes
/
Mondrian.py
File metadata and controls
20 lines (18 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#Mondrian algorithm
def mondrian(data, k, target):
targetVals = ["age","gender","zip"] # QI variables - hardcoded
partition = []
if len(data) <= 2 * k - 1:
return [data]
#sort by the predefined quasidenfier
data = data.sort_values(by=[targetVals[target]]) # Sorts with respect to age or zip depending on target
#define partition point
mid = len(data) // 2
#split the sorted data left and right
lhs = data[:mid]
rhs = data[mid:]
#splits each half until k anonymity condition is met
# Partition again wrt. next variable to split on...
partition.extend(mondrian(lhs, k, (target+1)%3))
partition.extend(mondrian(rhs, k, (target+1)%3))
return partition