-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
425 lines (359 loc) · 15.5 KB
/
Main.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
### Scripted by Mohammed Jasam
### mnqnd@mst.edu
import matplotlib.pyplot as plt
import numpy as np
import subprocess
import csv
import os
# resolves the directory issues on multiple systems!
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_pathAlgo=dir_path+"\Algorithms\ "
dir_pathOP=dir_path+"\Outputs\ "
l=[]
viz=[]
################################################ VISUALIZATION ###############################################
def Viz(l,v):
#Assigning x and y values from the list
a=l[0]
b=l[1]
#Creating a Plot
objects = ("Fold 1","Fold 2","Fold 3","Fold Mean")
y_pos = np.arange(len(objects))
fig, ax = plt.subplots()
bar_width = 0.3
opacity = 0.8
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='RMSE')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='MAE')
#Plot Properties
plt.xlabel('Folds')
plt.ylabel('Accuracy')
if v=='SVD':
plt.title('RMSE and MAE Values on Different Folds using SVD Algorithm')
elif v=='PMF':
plt.title('RMSE and MAE Values on Different Folds using PMF Algorithm')
elif v=='NMF':
plt.title('RMSE and MAE Values on Different Folds using NMF Algorithm')
elif v=='User':
plt.title('RMSE and MAE Values on Different Folds using User Algorithm')
elif v=='UserMSD':
plt.title('RMSE and MAE Values on Different Folds using User MSD Algorithm')
elif v=='UserCosine':
plt.title('RMSE and MAE Values on Different Folds using User Cosine Algorithm')
elif v=='UserPearson':
plt.title('RMSE and MAE Values on Different Folds using User Pearson Algorithm')
elif v=='UserK':
plt.title('RMSE and MAE Values on Different Folds using User K Algorithm')
elif v=='Item':
plt.title('RMSE and MAE Values on Different Folds using Item Algorithm')
elif v=='ItemMSD':
plt.title('RMSE and MAE Values on Different Folds using Item MSD Algorithm')
elif v=='ItemCosine':
plt.title('RMSE and MAE Values on Different Folds using Item Cosine Algorithm')
elif v=='ItemPearson':
plt.title('RMSE and MAE Values on Different Folds using Item Pearson Algorithm')
elif v=='ItemK':
plt.title('RMSE and MAE Values on Different Folds using Item K Algorithm')
plt.xticks(y_pos + bar_width, ("Fold 1","Fold 2","Fold 3","Fold Mean"))
plt.legend()
plt.tight_layout()
#Displaying the Plot
plt.show()
##################################################### VIZ COMPARE ######################################################
def VizCompare(FL,v):
#Extracting Values
a,b=FL[0],FL[1]
#Creating a plot
objects = ("a","b","c","d","e")#(1,2,3,4)
y_pos = np.arange(len(objects))
fig, ax = plt.subplots()
bar_width = 0.3
opacity = 0.8
#Plot Properties
plt.xlabel('Algorithms')
plt.ylabel('Value of RMSE & MAE')
if v=='f1':
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='RMSE')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='MAE')
plt.title('RMSE and MAE Values of Various Algorithms on Fold 1')
plt.xticks(y_pos + bar_width, ("SVD","PMF","NMF","User","Item"))
elif v=='f2':
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='RMSE')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='MAE')
plt.title('RMSE and MAE Values of Various Algorithms on Fold 2')
plt.xticks(y_pos + bar_width, ("SVD","PMF","NMF","User","Item"))
elif v=='f3':
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='RMSE')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='MAE')
plt.title('RMSE and MAE Values of Various Algorithms on Fold 3')
plt.xticks(y_pos + bar_width, ("SVD","PMF","NMF","User","Item"))
elif v=='fmean':
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='RMSE')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='MAE')
plt.title('RMSE and MAE Values of Various Algorithms on Mean of 3-Fold')
plt.xticks(y_pos + bar_width, ("SVD","PMF","NMF","User","Item"))
elif v=='RMSE':
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='User')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='Item')
plt.title('RMSE Values of User & Item Algorithm on Various Similarities')
plt.xticks(y_pos + bar_width, ("General","MSD","Cosine","Pearson","K"))
elif v=='MAE':
#Bars
rects1 = plt.bar(y_pos, a, bar_width,
alpha=opacity,
color='b',
label='User')
rects3 = plt.bar(y_pos + bar_width, b, bar_width,
alpha=opacity,
color='g',
label='Item')
plt.title('MAE Values of User & Item Algorithm on Various Similarities')
plt.xticks(y_pos + bar_width, ("General","MSD","Cosine","Pearson","K"))
plt.legend()
plt.tight_layout()
#Displaying Plot
plt.show()
################################################# End of Visualization ##################################################
################################################# Main Body ##################################################
##########---Generates Fold Performance of the Algorithms!!!---###########
#Code which is used to extract Folds!
def extract(filename,query):
v=[]
with open(filename, "r") as fp_in:
reader = csv.reader(fp_in, delimiter="\t")
header = next(reader)
for row in reader:
x=row[0]
l.append(x[8:].split())## Removes the Initial String
xa=[]
for i in range(len(l)):
xa.append([float(x) for x in l[i]])
v=xa
if query=='f1':
RMSE=0.0
MAE=0.0
for x in range(len(xa)):
if x==0:
RMSE=xa[x][0]
elif x==1:
MAE=xa[x][0]
del l[:]
return v,RMSE,MAE
elif query=='f2':
RMSE=0.0
MAE=0.0
for x in range(len(xa)):
if x==0:
RMSE=xa[x][1]
elif x==1:
MAE=xa[x][1]
del l[:]
return v,RMSE, MAE
elif query=='f3':
RMSE=0.0
MAE=0.0
for x in range(len(xa)):
if x==0:
RMSE=xa[x][2]
elif x==1:
MAE=xa[x][2]
del l[:]
return v,RMSE, MAE
elif query=='fmean':
RMSE=0.0
MAE=0.0
for x in range(len(xa)):
if x==0:
RMSE=xa[x][3]
elif x==1:
MAE=xa[x][3]
del l[:]
return v,RMSE, MAE
############################################## End of Main ##########################################
### Call the function name!!! ###
### The function returns RMSE and MAE Values!! ###
## To print, pass f1,f2,f3 or fmean below for respective FOLD values ###
################################################ Algorithms #############################################
print(" Calculating RMSE and MAE of SVD Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python SVD.py',shell=True)
os.chdir(dir_pathOP)
v,SVD_f1_a,SVD_f1_b=extract("SVD.csv",'f1') ### Parameters: f1=FOLD1 ; f2=FOLD2 ; f3=FOLD3 ; fmean=Mean of 3-FOLDS
v,SVD_f2_a,SVD_f2_b=extract("SVD.csv",'f2')
v,SVD_f3_a,SVD_f3_b=extract("SVD.csv",'f3')
v,SVD_fmean_a,SVD_fmean_b=extract("SVD.csv",'fmean')
Viz(v,'SVD')
print(" Calculating RMSE and MAE of PMF Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python PMF.py',shell=True)
os.chdir(dir_pathOP)
v,PMF_f1_a,PMF_f1_b=extract("PMF.csv",'f1') ### Parameters: f1=FOLD1 ; f2=FOLD2 ; f3=FOLD3 ; fmean=Mean of 3-FOLDS
v,PMF_f2_a,PMF_f2_b=extract("PMF.csv",'f2')
v,PMF_f3_a,PMF_f3_b=extract("PMF.csv",'f3')
v,PMF_fmean_a,PMF_fmean_b=extract("PMF.csv",'fmean')
Viz(v,'PMF')
print(" Calculating RMSE and MAE of NMF Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python NMF.py',shell=True)
os.chdir(dir_pathOP)
v,NMF_f1_a,NMF_f1_b=extract("NMF.csv",'f1') ### Parameters: f1=FOLD1 ; f2=FOLD2 ; f3=FOLD3 ; fmean=Mean of 3-FOLDS
v,NMF_f2_a,NMF_f2_b=extract("NMF.csv",'f2')
v,NMF_f3_a,NMF_f3_b=extract("NMF.csv",'f3')
v,NMF_fmean_a,NMF_fmean_b=extract("NMF.csv",'fmean')
Viz(v,'NMF')
print(" Calculating RMSE and MAE of User Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python User.py',shell=True)
os.chdir(dir_pathOP)
v,User_f1_a,User_f1_b=extract("User.csv",'f1') ### Parameters: f1=FOLD1 ; f2=FOLD2 ; f3=FOLD3 ; fmean=Mean of 3-FOLDS
v,User_f2_a,User_f2_b=extract("User.csv",'f2')
v,User_f3_a,User_f3_b=extract("User.csv",'f3')
v,User_fmean_a,User_fmean_b=extract("User.csv",'fmean')
Viz(v,'User')
print(" Calculating RMSE and MAE of Item Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python Item.py',shell=True)
os.chdir(dir_pathOP)
v,Item_f1_a,Item_f1_b=extract("Item.csv",'f1') ### Parameters: f1=FOLD1 ; f2=FOLD2 ; f3=FOLD3 ; fmean=Mean of 3-FOLDS
v,Item_f2_a,Item_f2_b=extract("Item.csv",'f2')
v,Item_f3_a,Item_f3_b=extract("Item.csv",'f3')
v,Item_fmean_a,Item_fmean_b=extract("Item.csv",'fmean')
Viz(v,'Item')
#################################################### End of Algorithm ########################################
############################################### Plotting Individual Folds #######################################
#Creating Seperate lists to hold the Fold Values!
Fold1=[]
Fold2=[]
Fold3=[]
FoldMeans=[]
# Appending the Fold Values to a list to visualize!
Fold1.append([SVD_f1_a,PMF_f1_a,NMF_f1_a,User_f1_a,Item_f1_a])
Fold1.append([SVD_f1_b,PMF_f1_b,NMF_f1_b,User_f1_b,Item_f1_b])
VizCompare(Fold1,'f1')
Fold2.append([SVD_f2_a,PMF_f2_a,NMF_f2_a,User_f2_a,Item_f2_a])
Fold2.append([SVD_f2_b,PMF_f2_b,NMF_f2_b,User_f2_b,Item_f2_b])
VizCompare(Fold2,'f2')
Fold3.append([SVD_f3_a,PMF_f3_a,NMF_f3_a,User_f3_a,Item_f3_a])
Fold3.append([SVD_f3_b,PMF_f3_b,NMF_f3_b,User_f3_b,Item_f3_b])
VizCompare(Fold3,'f3')
FoldMeans.append([SVD_fmean_a,PMF_fmean_a,NMF_fmean_a,User_fmean_a,Item_fmean_a])
FoldMeans.append([SVD_fmean_b,PMF_fmean_b,NMF_fmean_b,User_fmean_b,Item_fmean_b])
VizCompare(FoldMeans,'fmean')
###################################################### End of Plotting ###############################################
################################################# Collaborative Algorithms ####################################
print(" Calculating RMSE and MAE of User MSD Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python UserMSD.py',shell=True)
os.chdir(dir_pathOP)
v,User_MSD_a,User_MSD_b=extract("UserMSD.csv",'fmean')
Viz(v,'UserMSD')
print(" Calculating RMSE and MAE of User Cosine Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python UserCosine.py',shell=True)
os.chdir(dir_pathOP)
v,User_Cosine_a,User_Cosine_b=extract("UserCosine.csv",'fmean')
Viz(v,'UserCosine')
print(" Calculating RMSE and MAE of User Pearson Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python UserPearson.py',shell=True)
os.chdir(dir_pathOP)
v,User_Pearson_a,User_Pearson_b=extract("UserPearson.csv",'fmean')
Viz(v,'UserPearson')
print(" Calculating RMSE and MAE of User K Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python UserK.py',shell=True)
os.chdir(dir_pathOP)
v,User_K_a,User_K_b=extract("UserK.csv",'fmean')
Viz(v,'UserK')
print(" Calculating RMSE and MAE of Item MSD Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python ItemMSD.py',shell=True)
os.chdir(dir_pathOP)
v,Item_MSD_a,Item_MSD_b=extract("ItemMSD.csv",'fmean')
Viz(v,'ItemMSD')
print(" Calculating RMSE and MAE of Item Cosine Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python ItemCosine.py',shell=True)
os.chdir(dir_pathOP)
v,Item_Cosine_a,Item_Cosine_b=extract("ItemCosine.csv",'fmean')
Viz(v,'ItemCosine')
print(" Calculating RMSE and MAE of Item Pearson Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python ItemPearson.py',shell=True)
os.chdir(dir_pathOP)
v,Item_Pearson_a,Item_Pearson_b=extract("ItemPearson.csv",'fmean')
Viz(v,'ItemPearson')
print(" Calculating RMSE and MAE of Item K Algorithm")
print("====================================================================================")
os.chdir(dir_pathAlgo)
subprocess.call('python ItemK.py',shell=True)
os.chdir(dir_pathOP)
v,Item_K_a,Item_K_b=extract("ItemK.csv",'fmean')
Viz(v,'ItemK')
RMSE_UserItemGen=[]
MAE_UserItemGen=[]
# Appending the Fold Values to a list to visualize!
RMSE_UserItemGen.append([User_fmean_a,User_MSD_a,User_Cosine_a,User_Pearson_a,User_K_a])
RMSE_UserItemGen.append([Item_fmean_a,Item_MSD_a,Item_Cosine_a,Item_Pearson_a,Item_K_a])
MAE_UserItemGen.append([User_fmean_b,User_MSD_b,User_Cosine_b,User_Pearson_b,User_K_b])
MAE_UserItemGen.append([Item_fmean_b,Item_MSD_b,Item_Cosine_b,Item_Pearson_b,Item_K_b])
VizCompare(RMSE_UserItemGen,'RMSE')
VizCompare(MAE_UserItemGen,'MAE')
################################## End of Using various Similarities ######################################
############################################# Using K #####################################################
os.chdir(dir_path)
subprocess.call('python VariableKUserAndItem.py', shell=True)
############################################## End K #########################################################