-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindividual_project.py
290 lines (265 loc) · 9.12 KB
/
individual_project.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
import matplotlib.pyplot as plt
from pymining import itemmining, assocrules
from aprioriall import *
import numpy as np
# ======== Parameter Part (you can set) ======== #
ARM_MIN_SUPPORT = 500
DATASCALE_LIMIT = 300 # -1 for INF
CLUSTER_MIN_PRINT_SIZE = 10
CLUSTER_THRESHOLD = 0.4
N_OF_CLUSTERS = 150
DEFAULT_MODE = "groupavg" # "groupavg" or "complete" for clustering
PREPROCESSING = False # For removing users with one visiting page
user_dict = []
page_list = [-1]*1500
data_list = []
n_users = 0
n_pages = 0
dis_matrix = 0
dis_values = []
def readfile(filename):
global n_pages, n_users
file = open(filename, "r")
user_id = -1
cur_user_count = 0
for line in file.readlines():
line = line.replace("\"","")
line_list = line.split(",")
if(line_list[0]=='A'):
n_pages += 1
index = int(line_list[1])
page_list[index] = line_list[3]
if(line_list[0]=='C'):
if(DATASCALE_LIMIT!=-1 and n_users>=DATASCALE_LIMIT):
break
if(PREPROCESSING and cur_user_count==1):
user_dict.pop()
data_list.pop()
n_users -= 1
cur_user_count = 0
user_id = n_users
user_dict.append(int(line_list[2]))
data_list.append([])
n_users += 1
if(line_list[0]=='V'):
cur_user_count += 1
item = int(line_list[1])
data_list[user_id].append(item)
print("Done! User =",n_users,", Pages =",n_pages)
file.close()
# ==== ARM ====
def ARM(print_table = False):
visits = data_list
relim_input = itemmining.get_relim_input(visits)
report = itemmining.relim(relim_input, ARM_MIN_SUPPORT)
print("====REPORT====")
if(print_table):
print("* Top frequent visited pages *")
for itemset in report.keys():
if(len(itemset)==1):
t=next(iter(itemset))
print(t, report[itemset], page_list[t], sep='\t')
else:
print("Total number frequent itemsets:",len(report))
print(report)
rules1 = assocrules.mine_assoc_rules(report, min_support=ARM_MIN_SUPPORT, min_confidence=0.5)
print("====RULE====")
for line in rules1:
if (len(line[0])+len(line[1])>=4):
#print(line)
print("{",end='')
for i in line[0]:
print(str(i)+": "+str(page_list[i])+", ",end='')
print("}",end=' => ')
print("{",end='')
for i in line[1]:
print(str(i)+": "+str(page_list[i])+", ",end='')
print("}",end=', ')
print("Sup=",line[2],sep='',end=', ')
print("Conf=",line[3],sep='')
# ==== Sequential ARM ====
def generate_aprioriall_data(filename):
wfile = open(filename, "w")
assert n_users == len(data_list)
for i in range(n_users):
for item in data_list[i]:
print(item, file = wfile)
print("", file = wfile)
wfile.close()
def flat(nums):
res = []
for i in nums:
if isinstance(i, list):
res.extend(flat(i))
else:
res.append(i)
return res
def sequentialARM(input_filename, min_seq_len=2):
aa = AprioriAll(min_supp=0.02,datafile=input_filename)
litemset = aa.litemsetPhase()
print("litemset:")
print(litemset)
transmap = aa.createTransMap(litemset)
print("transformation map :")
print(transmap)
aa.transformationPhase(transmap)
customs = aa.customs
mapNums = []
for each in customs:
mapNums.append(each.getMapedNums())
seqNums = aa.sequencePhase(mapNums)
maxSeqs= aa.maxSeq(seqNums)
print("The sequential patterns :")
#print(maxSeqs)
for i in maxSeqs:
if(len(i)>=min_seq_len):
for j in flat(i):
print(j+": "+page_list[int(j)]+", ",end='')
print("")
# ==== User Clustering ====
def cal_distance(set1, set2):
set1 = set(set1)
set2 = set(set2)
return 1-len(set1&set2)/len(set1|set2)
def cal_dis_matrix():
print("Now calculating distance matrix ...")
global dis_matrix, dis_values
dis_values = []
dis_matrix = np.zeros([n_users,n_users], dtype=np.float)
for i in range(n_users):
if(i%100==0):
print(i,"...",end=" ")
for j in range(n_users):
dis_matrix[i,j]=cal_distance(data_list[i],data_list[j])
dis_matrix[j,i]=dis_matrix[i,j]
dis_values.append(dis_matrix[i,j])
print("Distance Matrix Calculated!")
dis_values.sort()
print("Distance Matrix Sorted!")
print("0%=",dis_values[0],", 25%=",dis_values[int(len(dis_values)*0.25)],", 50%=",dis_values[int(len(dis_values)*0.5)],", 75%=",dis_values[int(len(dis_values)*0.75)],", 100%=",dis_values[-1],sep='')
def linkage_distance(set1, set2, mode=DEFAULT_MODE):
dis = -1
if(mode=="complete"):
dis = -1
for i in set1:
for j in set2:
if(dis_matrix[i,j]>dis):
dis = dis_matrix[i,j]
if(mode=="groupavg"):
dis = 0
for i in set1:
for j in set2:
dis += dis_matrix[i,j]
dis = dis/(len(set1)*len(set2))
return dis
def detect_centroid(userset):
global data_list
min_sum_dis = -1
centroid_id = -1
for i in userset:
t_dis = 0
for j in userset:
if(i==j):
continue
t_dis += cal_distance(data_list[i], data_list[j])
if(t_dis<min_sum_dis or min_sum_dis==-1):
min_sum_dis = t_dis
centroid_id = i
return centroid_id
def clustering_users_with_threshold(threshold = CLUSTER_THRESHOLD):
#AGNES
cal_dis_matrix()
sset = []
#min_dis_list = []
#min_dis = 100
for i in range(n_users):
sset.append([i])
flag = True
t=0
print("Begin combining...")
while(flag):
flag = False
# Combine n-number_of_clusters times
n_sets = len(sset)
#min_dis = 100
combine_1 = -1
combine_2 = -1
for i in range(n_sets-1):
for j in range(i+1, n_sets):
t_dis = linkage_distance(sset[i], sset[j])
if(t_dis < threshold):
combine_1 = i
combine_2 = j
flag=True
break
if(flag):
break
sset[combine_1]=sset[combine_1]+sset[combine_2]
sset.remove(sset[combine_2])
t+=1
if(t%50==0):
print(t,end="...")
print("")
print("Total number of clusters:",n_users-t)
for s in sset:
if(len(s)>CLUSTER_MIN_PRINT_SIZE):
centroid = detect_centroid(s)
print("\nCluster centroid:",user_dict[centroid],"Cluster size:",len(s))
for i in data_list[centroid]:
print(i,": ",page_list[i],end=', ',sep='')
def clustering_users(n_of_clusters=N_OF_CLUSTERS):
#AGNES
cal_dis_matrix()
sset = []
min_dis_list = []
min_dis = 100
for i in range(n_users):
sset.append([i])
for t in range(n_users-n_of_clusters):
# Combine n-number_of_clusters times
n_sets = len(sset)
min_dis = 100
combine_1 = -1
combine_2 = -1
for i in range(n_sets-1):
for j in range(i+1, n_sets):
t_dis = linkage_distance(sset[i], sset[j])
if(t_dis < min_dis):
combine_1 = i
combine_2 = j
min_dis = t_dis
sset[combine_1]=sset[combine_1]+sset[combine_2]
sset.remove(sset[combine_2])
min_dis_list.append(min_dis)
if(t%50==0):
print(t,end="...")
print("")
print("Total number of clusters:",n_users-t-1)
#print(min_dis_list)
plt.plot(min_dis_list)
plt.show()
'''
for i in min_dis_list:
print(round(i,2),end=", ")
'''
print("")
for s in sset:
if(len(s)>CLUSTER_MIN_PRINT_SIZE):
centroid = detect_centroid(s)
print("\nCluster centroid:",user_dict[centroid],"Cluster size:",len(s))
for i in data_list[centroid]:
print(i,": ",page_list[i],end=', ',sep='')
# ======== Running Part (comment the function you don't need) ======== #
# readfile: compulsory data reading and processing function
readfile("C:/Users/54611/OneDrive/Subject Files/COMP4433 Data Mining and Data Warehousing/Individual/anonymous-msweb/anonymous-msweb.data")
# --- ARM: for association rule mining
ARM()
# --- generate_aprioriall_data: compulsory for AprioriAll method
# --- sequentialARM: call AprioriAll for sequential ARM
generate_aprioriall_data("C:/Users/54611/OneDrive/Subject Files/COMP4433 Data Mining and Data Warehousing/Individual/anonymous-msweb/aprioriall_input.txt")
sequentialARM("C:/Users/54611/OneDrive/Subject Files/COMP4433 Data Mining and Data Warehousing/Individual/anonymous-msweb/aprioriall_input.txt")
# --- clustering_users: clustering method (time and space complexity is too high for 30000+ data)
clustering_users()
# --- clustering_users_with_threshold: simplified method
clustering_users_with_threshold()
# ==================================================================== #